How should shell scripts be used?

PHP中文网
Release: 2017-06-21 14:17:35
Original
3743 people have browsed it

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.

Shell application scenarios

What Shell can do

  • 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

What Shell can’t do

  • 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


How Shell works

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.


The simplest script:

#!/bin/bashecho "Hello World"
Copy after login

Just open the text editing tool, edit it as above, and then save it as test.sh

Run the script:

1. cd 到该目录下2. chmod +x ./test.sh  #给脚本权限3. ./test.sh  #执行脚本
Copy after login

Rendering 1

In this way we wrote the first The simplest script, below we can try to write some complex scripts.


Variables in Shell

myText="hello world"muNum=100
Copy after login

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.

Access variables

myText="hello world"muNum=100echo $myTextecho muNum
Copy after login

When you want to access variables, you need to use $, otherwise the output will be plain text, as shown in the figure below.


Rendering 2

Four arithmetic operations in Shell

Example program

#!/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"
Copy after login

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.


Rendering 3.png

Other operators =,==,!=,! , -o, -a

Example program

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
Copy after login

Rendering 4

Relational operators

!/bin/shExample program

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
Copy after login

Rendering 5

字符串运算符





字符串

#!/bin/shmtext="hello"  #定义字符串
mtext2="world"mtext3=$mtext" "$mtext2  #字符串的拼接echo $mtext3  #输出字符串echo ${#mtext3}  #输出字符串长度echo ${mtext3:1:4}  #截取字符串
Copy after login

效果图6

数组

#!/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
Copy after login

效果图7

输出程序

echo

#!/bin/shecho "hello world"  echo hello world  

text="hello world"echo $textecho -e "hello \nworld"  #输出并且换行echo "hello world" > a.txt  #重定向到文件echo `date`  #输出当前系统时间
Copy after login

效果图8

printf

同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
Copy after login
效果图9

test命令

test $[num1] -eq $[num2]  #判断两个变量是否相等
test num1=num2  #判断两个数字是否相等
Copy after login

 

for循环
#!/bin/shfor i in {1..5}doecho $idonefor i in 5 6 7 8 9doecho $idonefor FILE in $HOME/.bash*do
   echo $FILEdone
Copy after login
<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>
Copy after login
效果10

while循环

#!/bin/shCOUNTER=0while [ $COUNTER -lt 5 ]doCOUNTER=`expr $COUNTER + 1`echo $COUNTERdoneecho &#39;请输入。。。&#39;echo &#39;ctrl + d 即可停止该程序&#39;while read FILMdoecho "Yeah! great film the $FILM"done
Copy after login

以上是while循环的两种用法,第一种是比较常规的,执行循环,然后每次都把控制的数加1,就可以让while循环有退出的条件了。

第二种是用户从键盘数据,然后把用户输入的文字输出出来。


跳出循环

break  #跳出所有循环
break n  #跳出第n层f循环
continue  #跳出当前循环
Copy after login

函数

#!/bin/shsysout(){echo "hello world"}

sysout
Copy after login

定义一个没有返回值的函数,然后调用该函数

#!/bin/shtest(){

    aNum=3anotherNum=5return $(($aNum+$anotherNum))
}
test
result=$?echo $result
Copy after login

定义一个有返回值的函数,调用该函数,输出结果


效果图11
#!/bin/shtest(){echo $1  #接收第一个参数echo $2  #接收第二个参数echo $3  #接收第三个参数echo $#  #接收到参数的个数echo $*  #接收到的所有参数
}

test aa bb cc
Copy after login

定义了一个需要传递参数的函数


效果图12

重定向

$echo result > file  #将结果写入文件,结果不会在控制台展示,而是在文件中,覆盖写
$echo result >> file  #将结果写入文件,结果不会在控制台展示,而是在文件中,追加写echo input < file  #获取输入流
Copy after login


写一个自动输入命令的脚本

自动提交github仓库的脚本

#!/bin/bashecho "-------Begin-------"git add .
git commit -m $1echo $1git push origin masterecho "--------End--------"
Copy after login

 


效果图13

以上便是我对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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template