当前位置:网站首页>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
边栏推荐
- ffmpeg AVFrame 转 cv::Mat
- ES6中的代理proxy
- Detailed explanation of JUnit unit test framework
- Ffmpeg error code
- CMU AI PhD 第一年总结
- Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
- Ffmpeg avframe to cv:: mat
- [to.Net] C set class source code analysis
- Write it down once Net travel management background CPU Explosion Analysis
- Parallelism, concurrency and life cycle of threads
猜你喜欢
[Mori city] random talk on GIS data (I)
精耕渠道共謀發展 福昕攜手偉仕佳傑開展新產品培訓大會
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Actual combat of flutter - fast implementation of audio and video call application
wireshark报文分析tcp,ftp
论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
XML syntax, constraints
【To .NET】C#集合类源码解析
随机推荐
Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework
[pytorch record] automatic hybrid accuracy training torch cuda. amp
Parallelism, concurrency and life cycle of threads
EasyGBS主子码流都为H.265时,切换出现花屏如何解决?
DTD建模
Download (export) PDF template file (such as approval form), and report error: invalid nested tag * * * found, expected closing tag***
[6.24-7.1] review of wonderful technical blog posts in the writing community
Learning records of building thingsboard, an Internet of things platform
Opencv video quality detection -- sharpness detection
Lean thinking: source, pillar, landing. I understand it after reading this article
音频编解码基础知识
The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site
Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits
pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
学习笔记【gumbel softmax】
axure不显示元件库
直播HLS协议
ubuntu14安装MySQL并配置root账户本地与远程访问
如何正确使用Vertx操作Redis(3.9.4带源码分析)