当前位置:网站首页>Shell: basic learning
Shell: basic learning
2022-07-03 12:05:00 【J_ D_ Chi】
List of articles
Write it at the front
shell Scripting language is similar to chatting with several people . You just need to think of all the orders as people who can help you , As long as you ask them to do it in the right way .
~ % who am I
jdchi ttys000 Jul 29 10:33
~ % date
2021 year 7 month 29 Japan Thursday 10 when 40 branch 27 second CST
first Shell Script
#!/bin/bash
echo "Hello World"
#! It's a convention mark , Tell the system what interpreter this script needs
function Shell Script
chmod +x ./test.sh # Give the script permission to execute
./test.sh # Execute the script
./ Tell the system to find in the current directory , Otherwise the system will go PATH Look in the catalog .
Getting input from the keyboard
#!/bin/bash
echo "What's your name"
read NAME
echo "My name is $NAME"
Assign keyboard input to variables NAME
shell % chmod +x ./test2.sh
shell % ./test2.sh
What's your name
JD
My name is JD
Multiline comment
:<<EOF notes notes notes EOF
Shell Variable
There can be no spaces between variable names and equal signs !
A read-only variable
#!/bin/bash
myName="hello"
readonly myName
myName="hi"
./shell_var.sh: line 5: myName: readonly variable
Delete variables
#!/bin/bash
hello="hello"
unset hello
echo $hello
There is no output
Variable type
- local variable
- Define... In a script or command , At present only shell Valid in instance , Other shell Started program cannot access local variables
- environment variable
- All programs can be accessed , When necessary shell Scripts can also define environment variables
- shell Variable
- shell Special variables for program settings
Shell Array
Supports one dimensional array
Define an array
Use parentheses to represent arrays , Separate... With spaces .
#!/bin/bash
array=(1 2 3 4)
echo ${array[0]}
Read array
# Read an element
value=${array[1]}
# Read all the elements in the array
echo ${array[*]}
echo ${array[@]}
Get array length
echo ${
#array[*]}
Shell Pass parameters
When executing the script , Pass parameters to script , The format of getting parameters in the script is :$n,n On behalf of the n Parameters .
$0 For the filename of the execution
#!/bin/bash
echo " The file named $0"
echo " The first parameter is zero $1"
echo " The second parameter is $2"
shell % ./shell_args.sh aa bb
The file named ./shell_args.sh
The first parameter is zero aa
The second parameter is bb
Special characters
echo " The number of parameters is $#"
echo " Display all parameters in a single string $*"
echo " The current process ID by $$"
echo " The last process running in the background ID by $!"
echo "Shell Currently used options $-"
# 0 No mistakes
echo " The exit state of the last command $?"
Shell Basic operators
Native bash Simple mathematical operations are not supported , But it can be achieved by other commands , for example awk and expr.
Arithmetic operator
expr Is an expression calculation tool , It can be used to evaluate expressions .
#!/bin/bash
a=10
b=20
echo a = $a
echo b = $b
echo a + b = `expr $a + $b`
echo a - b = `expr $a - $b`
shell % ./cal.sh
a = 10
b = 20
a + b = 30
a - b = -10
Relational operator
#!/bin/bash
a=10
b=20
echo a = $a
echo b = $b
# Whether two numbers are equal
if [ $a -eq $b ]
then
echo a And b equal
else
echo a And b It's not equal
fi
# Whether the two numbers are not equal
if [ $a -ne $b ]
then
echo a And b It's not equal
else
echo a And b equal
fi
# Whether the number on the left is greater than that on the right
if [ $a -gt $b ]
then
echo a Than b Big
else
echo a Than b Small
fi
# Whether the number on the left is less than that on the right
if [ $a -lt $b ]
then
echo a Than b Small
else
echo a Than b Big
fi
# Whether the number on the left is greater than or equal to that on the right
if [ $a -ge $b ]
then
echo a Greater than or equal to b
else
echo a Less than b
fi
# Whether the number on the left is less than or equal to that on the right
if [ $a -le $b ]
then
echo a Less than or equal to b
else
echo a Greater than b
fi
shell % ./rel_operator.sh
a = 10
b = 20
a And b It's not equal
a And b It's not equal
a Than b Small
a Than b Small
a Less than b
a Less than or equal to b
Boolean operator
#!/bin/bash
a=10
b=100
# Non operation
if [ a != b ]
then
echo a It's not equal to b
else
echo a be equal to b
fi
# And operation
if [ $a -eq 10 -a $b -eq 100 ]
then
echo a be equal to 10 b be equal to 100
else
echo a or b Not equal to the initial value
fi
# Or operations
if [ $a -eq 10 -o $b -eq 100 ]
then
echo a be equal to 10 or b be equal to 100
else
echo a and b Not equal to the initial value
fi
shell % ./bool_operator.sh
a It's not equal to b
a be equal to 10 b be equal to 100
a be equal to 10 or b be equal to 100
String operators
#!/bin/bash
a='abc'
b='def'
if [ $a = $b ]
then
echo a And b equal
else
echo a And b It's not equal
fi
if [ $a != $b ]
then
echo a And b It's not equal
else
echo a And b equal
fi
# Check if the string length is 0
if [ -z $a ]
then
echo a The length of is 0
else
echo a The length is not 0
fi
# Check whether the string length is not 0
if [ -n $b ]
then
echo b The length is not 0
else
echo b The length of is 0
fi
# Check if the string is empty
if [ $a ]
then
echo a Not empty
else
echo a It's empty
fi
shell % ./string_operator.sh
a And b It's not equal
a And b It's not equal
a The length is not 0
b The length is not 0
a Not empty
File test operators
#!/bin/bash
file="cal.sh"
if [ -r $file ]
then
echo Documents are readable
else
echo The file is unreadable
fi
if [ -w $file ]
then
echo Documents can be written
else
echo The document is not writable
fi
if [ -x $file ]
then
echo The document is executable
else
echo The file is not executable
fi
if [ -f $file ]
then
echo The file is a normal file
else
echo Documents are special documents
fi
if [ -d $file ]
then
echo The file is a directory
else
echo File is not a directory
fi
if [ -s $file ]
then
echo The file is not empty
else
echo The file is empty
fi
if [ -e $file ]
then
echo File exists
else
echo file does not exist
fi
shell % ./file_operator.sh
Documents are readable
Documents can be written
The document is executable
The file is a normal file
File is not a directory
The file is not empty
File exists
Shell print command
printf Command imitation C library , Transplantability ratio echo good .
grammar :printf format-string [arguments…]
printf "%-10s %-8s %-4.2f\n" Guo Jing male 66.1234
Guo Jing male 66.12
%-10s A width of 10 Characters (- Indicates left alignment , If not, it means right alignment ), Any character will be displayed in 10 Within characters wide , If it is not enough, it will be automatically filled with spaces , More than will also show all the content .
%-4.2f It means to format it as a decimal , among .2 Finger retention 2 Decimal place .
Shell test command
test The command is used to check if a condition holds .
num1=100
num2=100
if test $[num1] -eq $[num2]
then
echo ' Two numbers are equal !'
else
echo ' The two numbers are not equal !'
fi
Two numbers are equal !
Shell Process control
for loop
Format
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
Example
#!/bin/bash
array=(1 2 3 4 5)
for i in ${array[@]}
do
echo $i
done
shell % ./process_control.sh
1
2
3
4
5
while sentence
Format
while condition
do
command
done
Example
#!/bin/bash
a=0
while (($a<=5))
do
echo $a
let "a++"
done
0
1
2
3
4
5
case … esac
Be similar to switch case, Every case The branch begins with a right parenthesis , Use two semicolons ;; Express break, use esac End mark .
Format
case value in
Pattern 1)
command1
command2
...
commandN
;;
Pattern 2)
command1
command2
...
commandN
;;
esac
Example
#!/bin/bash
echo Input number
read num
case $num in
1) echo I chose 1
;;
2) echo I chose 2
;;
*) echo Other selected
;;
esac
Shell function
Format
[ function ] funname [()]
{
action;
[return int;]
}
Example
#!/bin/bash
fun(){
echo Custom function
}
fun
funWithParam(){
echo The first parameter is zero $1
echo The second parameter is $2
}
funWithParam 1 3
funWithReturn(){
return 1
}
funWithReturn2(){
return 2
}
funWithReturn
echo The return value is $?
funWithReturn2
echo The return value is $?
shell % ./fun.sh
Custom function
The first parameter is zero 1
The second parameter is 3
The return value is 1
The return value is 2
Shell Input / Output redirection
majority UNIX The system command receives the input from your terminal and sends the output back to your terminal . A command usually reads input from a place called standard input , By default , This happens to be your terminal . Again , A command usually writes its output to standard output , By default , This is also your terminal .
Output redirection
shell % touch myfile
shell % who > myfile
shell % echo 'hello' > myfile
shell % cat myfile
hello
shell % echo 'hello again' >> myfile
shell % cat myfile
hello
hello again
Input redirection
shell % wc -l myfile
2 myfile
shell % wc -l < myfile
2
Reference resources
边栏推荐
- MCDF Experiment 1
- ArcGIS application (XXI) ArcMap method of deleting layer specified features
- Ripper of vulnhub
- Pragma pack syntax and usage
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- Concurrent programming - singleton
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- Vulnhub geminiinc
- 小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
- OpenGL draws colored triangles
猜你喜欢

小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求

vulnhub之tomato(西红柿)

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

Introduction to the implementation principle of rxjs observable filter operator

PHP导出word方法(一mht)

rxjs Observable filter Operator 的实现原理介绍

vulnhub之Nagini

Talk about the state management mechanism in Flink framework

VS2015的下载地址和安装教程

Groovy test class and JUnit test
随机推荐
优化接口性能
PHP Basics
Unity3d learning notes 5 - create sub mesh
(数据库提权——Redis)Redis未授权访问漏洞总结
DNS多点部署IP Anycast+BGP实战分析
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Nestjs configuration service, configuring cookies and sessions
Vulnhub's Tomato (tomato)
"Jianzhi offer 04" two-dimensional array search
PHP導出word方法(一mht)
SLF4J 日志门面
Systemverilog-- OOP--对象的拷贝
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Groovy测试类 和 Junit测试
Kubernetes three dozen probes and probe mode
023(【模板】最小生成树)(最小生成树)
Cacti监控Redis实现过程
Deploying WordPress instance tutorial under coreos
(构造笔记)MIT reading部分学习心得
Notes on 32-96 questions of sword finger offer