Shell script uses the command interpretation function of Shell to parse a plain text file and then execute these functions. It can also be said that Shell script is a collection of a series of commands.
Shell can be used directly on win/Unix/Linux, and can call a large number of internal system functions to interpret and execute programs. If you are proficient in Shell scripts, it will make it easier for us to operate the computer and save a lot of time.
Simplify some complex commands (usually we may need to submit the github code once There are many steps, but it can be simplified into one step using Shell)
You can write some scripts to automatically replace the latest sdk (library) in a project
Automatic packaging, compilation, publishing and other functions
Clean up empty folders in the disk
In short, you can try all regular live scripts
When precise calculations are required
The language needs to be very efficient When it is high
When some network operations are required
In short, Shell can quickly develop a script to simplify the development process, and cannot be used Alternative high-level language
Shell can be called a scripting language because it does not require compilation itself, but is interpreted through After the compiler interprets it, it compiles and executes it. Compared with traditional languages, there is an extra interpretation process, so the efficiency will be slightly worse than traditional directly compiled languages.
#!/bin/bashecho "Hello World"
Just open the text editing tool, edit it as above, and then save it as test.sh
1. cd 到该目录下2. chmod +x ./test.sh #给脚本权限3. ./test.sh #执行脚本
In this way we wrote the first The simplest script, below we can try to write some complex scripts.
myText="hello world"muNum=100
What needs to be noted here is that there cannot be spaces before and after "=", and the naming rules are the same as in other languages.
myText="hello world"muNum=100echo $myTextecho muNum
When you want to access variables, you need to use $, otherwise the output will be plain text, as shown in the figure below.
#!/bin/bashecho "Hello World !"a=3b=5val=`expr $a + $b`echo "Total value : $val"val=`expr $a - $b`echo "Total value : $val"val=`expr $a \* $b`echo "Total value : $val"val=`expr $a / $b`echo "Total value : $val"
What needs to be noted here is that when defining a variable, there cannot be spaces before and after "=", but when performing four arithmetic operations, there must be spaces before and after the operation symbols. When doing multiplication, Needs to be escaped.
a=3b=5val=`expr $a / $b`echo "Total value : $val"val=`expr $a % $b`echo "Total value : $val"if [ $a == $b ]then echo "a is equal to b"fiif [ $a != $b ]then echo "a is not equal to b"fi
a=10b=20if [ $a -eq $b ]then echo "true"else echo "false"fiif [ $a -ne $b ]then echo "true"else echo "false"fiif [ $a -gt $b ]then echo "true"else echo "false"fiif [ $a -lt $b ]then echo "true"else echo "false"fiif [ $a -ge $b ]then echo "true"else echo "false"fiif [ $a -le $b ]then echo "true"else echo "false"fi
#!/bin/shmtext="hello" #定义字符串 mtext2="world"mtext3=$mtext" "$mtext2 #字符串的拼接echo $mtext3 #输出字符串echo ${#mtext3} #输出字符串长度echo ${mtext3:1:4} #截取字符串
#!/bin/sharray=(1 2 3 4 5) #定义数组 array2=(aa bb cc dd ee) #定义数组 value=${array[3]} #找到某一个下标的数,然后赋值echo $value #打印 value2=${array2[3]} #找到某一个下标的数,然后赋值echo $value2 #打印 length=${#array[*]} #获取数组长度echo $length
#!/bin/shecho "hello world" echo hello world text="hello world"echo $textecho -e "hello \nworld" #输出并且换行echo "hello world" > a.txt #重定向到文件echo `date` #输出当前系统时间
同c语言,就不过多介绍了
if
if-else
if-elseIf
case
#!/bin/sha=10b=20if [ $a == $b ]then echo "true"fiif [ $a == $b ]then echo "true"else echo "false"fiif [ $a == $b ]then echo "a is equal to b"elif [ $a -gt $b ]then echo "a is greater than b"elif [ $a -lt $b ]then echo "a is less than b"else echo "None of the condition met"fi
test $[num1] -eq $[num2] #判断两个变量是否相等 test num1=num2 #判断两个数字是否相等
for循环
#!/bin/shfor i in {1..5}doecho $idonefor i in 5 6 7 8 9doecho $idonefor FILE in $HOME/.bash*do echo $FILEdone
<code class="bash"><span class="hljs-meta"><br/><img src="https://img.php.cn/upload/article/000/000/001/e287dce1bca30a9896dc8bd2ecb0e156-15.png" alt=""/></span></code>
#!/bin/shCOUNTER=0while [ $COUNTER -lt 5 ]doCOUNTER=`expr $COUNTER + 1`echo $COUNTERdoneecho '请输入。。。'echo 'ctrl + d 即可停止该程序'while read FILMdoecho "Yeah! great film the $FILM"done
以上是while循环的两种用法,第一种是比较常规的,执行循环,然后每次都把控制的数加1,就可以让while循环有退出的条件了。
第二种是用户从键盘数据,然后把用户输入的文字输出出来。
break #跳出所有循环 break n #跳出第n层f循环 continue #跳出当前循环
#!/bin/shsysout(){echo "hello world"} sysout
定义一个没有返回值的函数,然后调用该函数
#!/bin/shtest(){ aNum=3anotherNum=5return $(($aNum+$anotherNum)) } test result=$?echo $result
定义一个有返回值的函数,调用该函数,输出结果
#!/bin/shtest(){echo $1 #接收第一个参数echo $2 #接收第二个参数echo $3 #接收第三个参数echo $# #接收到参数的个数echo $* #接收到的所有参数 } test aa bb cc
定义了一个需要传递参数的函数
$echo result > file #将结果写入文件,结果不会在控制台展示,而是在文件中,覆盖写 $echo result >> file #将结果写入文件,结果不会在控制台展示,而是在文件中,追加写echo input < file #获取输入流
#!/bin/bashecho "-------Begin-------"git add . git commit -m $1echo $1git push origin masterecho "--------End--------"
以上便是我对shell知识的总结,欢迎大家点心,评论,一起讨论~~
The above is the detailed content of How should shell scripts be used?. For more information, please follow other related articles on the PHP Chinese website!