当前位置:网站首页>Shell advanced
Shell advanced
2022-07-01 19:37:00 【Upward Wolf】
One 、Shell Operator
1.1、 Classification of operators
- Arithmetic operator
- Relational operator
- Boolean operator
- String operators
- File test operators
1.2、 Arithmetic operator
#!/bin/basha=10b=20val=`expr $a + $b` notes : here = No spaces on either side of the sign , And that is + There should be spaces on the left and right , The following are the same rulesecho "a + b : $val"val=`expr $a - $b` notes : Used here ` Is in Esc The symbol belowecho "a - b : $val"val=`expr $a \* $b`echo "a * b : $val"val=`expr $b / $a`echo "b / a : $val"val=`expr $b % $a`echo "b % a : $val"if [ $a == $b ] notes : if Follow [ There should be spaces between , [ Follow $a There are also spaces between , Back $b Follow ] Spaces are also requiredthenecho "a be equal to b"fiif [ $a != $b ]thenecho "a It's not equal to b"fi
1.3、 Relational operator
#!/bin/basha=10b=20if [ $a -eq $b ]thenecho "$a -eq $b : a be equal to b"elseecho "$a -eq $b: a It's not equal to b"fiif [ $a -ne $b ]thenecho "$a -ne $b: a It's not equal to b"elseecho "$a -ne $b : a be equal to b"fiif [ $a -gt $b ]thenecho "$a -gt $b: a Greater than b"elseecho "$a -gt $b: a No more than b"fiif [ $a -lt $b ]thenecho "$a -lt $b: a Less than b"elseecho "$a -lt $b: a Not less than b"fiif [ $a -ge $b ]thenecho "$a -ge $b: a Greater than or equal to b"elseecho "$a -ge $b: a Less than b"fiif [ $a -le $b ]thenecho "$a -le $b: a Less than or equal to b"elseecho "$a -le $b: a Greater than b"fi
1.4、 Boolean operator
#!/bin/basha=10b=20if [ $a != $b ]thenecho "$a != $b : a It's not equal to b"elseecho "$a == $b: a be equal to b"fiif [ $a -lt 100 -a $b -gt 15 ]thenecho "$a Less than 100 And $b Greater than 15 : return true"elseecho "$a Less than 100 And $b Greater than 15 : return false"fiif [ $a -lt 100 -o $b -gt 100 ]thenecho "$a Less than 100 or $b Greater than 100 : return true"elseecho "$a Less than 100 or $b Greater than 100 : return false"fiif [ $a -lt 5 -o $b -gt 100 ]thenecho "$a Less than 5 or $b Greater than 100 : return true"elseecho "$a Less than 5 or $b Greater than 100 : return false"fi
1.5、 Logical operators
#!/bin/basha=10b=20if [[ $a -lt 100 && $b -gt 100 ]] notes : If the logical operator is used here &&, || You need two [[ , ]], Ifthen If you use bitwise operators, you don't need .echo " return true"elseecho " return false"fiif [[ $a -lt 100 || $b -gt 100 ]]thenecho " return true"elseecho " return false"fi
1.6、 String operators
#!/bin/basha="abc"b="efg"if [ $a = $b ] // Be careful : The comparison of strings here is = Numberthenecho "$a = $b : a be equal to b"elseecho "$a = $b: a It's not equal to b"fiif [ $a != $b ]thenecho "$a != $b : a It's not equal to b"elseecho "$a != $b: a be equal to b"fiif [ -z $a ]thenecho "-z $a : String length is 0"3.1.6. File test operatorselseecho "-z $a : String length is not 0"fiif [ -n "$a" ]thenecho "-n $a : String length is not 0"elseecho "-n $a : String length is 0"fiif [ $a ]thenecho "$a : String is not empty "elseecho "$a : The string is empty "fi
1.7、 File test operators
#!/bin/bashfile="/var/node/test.sh"if [ -r $file ]thenecho " Documents are readable "elseecho " The file is unreadable "fiif [ -w $file ]thenecho " Documents can be written "elseecho " The document is not writable "fiif [ -x $file ]thenecho " The document is executable "elseecho " The file is not executable "fiif [ -f $file ]thenecho " The file is a normal file "elseecho " Documents are special documents "fiif [ -d $file ]thenecho " A file is a directory "elseecho " File is not a directory "fiif [ -s $file ]thenecho " The file is not empty "elseecho " The file is empty "fiif [ -e $file ]thenecho " File exists "elseecho " file does not exist "fi
Two 、echo Print data
Shell Of echo Instructions are used for the output of strings
## Show normal stringecho "Hello World"## Show escape charactersecho "\"Hello World\""## Show variablename="zhangsan"echo "$name Hello World"## Show wrapecho -e "OK! \n" notes : If you want to use the line feed function , You need to in echo Add one more -eecho "Hello World"## Show no line breaksecho -e "OK! \c"echo "Hello World"## Show results directed to fileecho "Hello World" > myfile## Output string as isecho '$name\"'## Show command execution resultsecho `date`
3、 ... and 、test command
- Numbers
- character string
- File test
# Comparenum1=100num2=100if test $[num1] -eq $[num2]thenecho ' Two numbers are equal !'elseecho ' The two numbers are not equal !'fi
Four 、Shell Process control ( Can be associated java grammar )
4.1、if
Format :
if condition1thencommand1elif condition2thencommand2elsecommandNfi
give an example :
a=10b=20if [ $a == $b ]thenecho "a be equal to b"elif [ $a -gt $b ]thenecho "a Greater than b"elif [ $a -lt $b ]thenecho "a Less than b"elseecho " There are no conditions that meet "fi
4.2、case
case value inPattern 1)command1command2...commandN;;Pattern 2)command1command2...commandN;;esac
give an example :
echo ' Input 1 To 4 Number between :'echo ' The number you enter is :'read numcase $num in1) echo ' You chose 1';;2) echo ' You chose 2';;3) echo ' You chose 3';;4) echo ' You chose 4';;*) echo ' You didn't enter 1 To 4 Number between ';;esac
4.3、for
- When variable values are in the list ,for A loop executes all the commands at once , Use the variable name to get the current value in the list .
- The command can be any valid shell Commands and statements .in The list can contain substitutions 、 String and file name .
- in List is optional , If you don't use it ,for Loop through the positional parameters of the command line .
Format :
for var in item1 item2 ... itemNdocommand1command2...commandNdone
give an example :
for loop in 1 2 3 4 5doecho "The value is: $loop"donefor str in 'This is a string','hello moto'doecho $strdone
4.4、while loop
while Loop is used to execute a series of commands continuously , It is also used to read data from the output file ; Commands are usually test conditions
while conditiondocommanddone
# Bash let command , It is used to execute one or more expressions , There is no need to add $ To represent a variable#!/bin/bashint=1while(( $int<=5 )) Be careful : If you want to use <=, >= Operator to use (( )) To writedoecho $intlet "int++"done# Infinite loopwhile truedocommanddone
4.5、break
break Command run jumps out of all loops ( Terminate all subsequent operations )
#!/bin/bashwhile :doecho -n " Input 1 To 5 Number between :"read aNumcase $aNum in1|2|3|4|5) echo " The number you enter is $aNum!";;*) echo " The number you entered is not 1 To 5 Between ! Game over "break;;esacdone
4.7、continue
continue The command will not jump out of all loops , Just jump out of the current loop
#!/bin/bashwhile :doecho -n " Input 1 To 5 Number between : "read aNumcase $aNum in1|2|3|4|5) echo " The number you enter is $aNum!";;*) echo " The number you entered is not 1 To 5 Between !"continueecho " Game over ";;esacdone
5、 ... and 、Shell function
- Linux shell You can customize functions , And then in shell You can call... In the script .
- You can take function fun() Definition , It can also be direct fun() Definition , Without any parameters .
- Parameter return , Can display plus :return return , If not , Results will be run with the last command , As return value . return Heel value n(0-255)
#!/bin/bash## The first function ------------------------------demoFun(){echo " This is my first shell function !"}echo "----- The function starts executing -----"demoFunecho "----- The function is finished -----"## Function return value ------------------------------funWithReturn(){echo " This function will add two input numbers ..."echo " Enter the first number : "read aNumecho " Enter the second number : "read anotherNumecho " The two figures are $aNum and $anotherNum !"return $(($aNum+$anotherNum))}funWithReturn# The return value of the function passes through $? To obtain a .echo " The sum of the two numbers entered is $? !"## Function parameter ------------------------------funWithParam(){echo " The first parameter is zero $1 !"echo " The second parameter is $2 !"echo " The tenth parameter is $10 !"echo " The tenth parameter is ${10} !"}funWithParam 1 2 3 4 5 6 7 8 9
边栏推荐
- optaplanner学习笔记(一)案例Cloud balance
- 白盒加密技术浅理解
- 见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
- Enabling "new Chinese enterprises", SAP process automation landing in China
- 【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
- EasyGBS主子码流都为H.265时,切换出现花屏如何解决?
- Shell高级进阶
- [pytorch record] automatic hybrid accuracy training torch cuda. amp
- IPv4 address, subnet mask, gateway
- MATLAB中subplot函数的使用
猜你喜欢
Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
智慧防疫系统为建筑工地复工复产提供安全保障
使用环信提供的uni-app Demo,快速实现一对一单聊
Crunch简介、安装,使用Crunch制作密码字典
精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
MATLAB中subplot函数的使用
奔赴山海之前,毕业季一定要做的那些事情
为什么一定要从DevOps走向BizDevOps?
随机推荐
ffmpeg常用命令(二)
[go ~ 0 to 1] day 4 June 30 defer, structure, method
Learn MySQL from scratch - database and data table operations
Methods of finding various limits
axure不显示元件库
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
物联网平台thingsboard搭建学习记录
宝,运维100+服务器很头疼怎么办?用行云管家!
智慧防疫系统为建筑工地复工复产提供安全保障
采集抖音视频
奔赴山海之前,毕业季一定要做的那些事情
Detailed explanation of JUnit unit test framework
MFC中如何重绘CListCtrl的表头
学习笔记【gumbel softmax】
Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
Extensive reading of the paper [film: visual reasoning with a general condition layer]
IPv4 address, subnet mask, gateway
面试题 16.16. 部分排序-双指针法
下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
[to.Net] C set class source code analysis