catalogue
Shell operator
-
Arithmetic operator
-
#! /bin/bash a=20 b=10 val=`expr $a + $b`#Not a single quotation mark, but a symbol under esc (English input method) echo "a + b : $val" val=`expr $a - $b` echo "a - b : $val" val=`expr $a \* $b` echo "a * b : $val" val=`expr $a / $b` echo "a / b : $val" val=`expr $a % $b` echo "a + b : $val" if [ $a == $b ] then echo "a be equal to b" else echo "a Not equal to b" fi if [ $a != $b ] then echo "a Not equal to b" else echo "a be equal to b" fi
- expr is an expression calculation tool, which can be used to complete the evaluation of expressions
-
- Relational operator
-
- Relational operators only support numbers, not strings, unless the value of the string is a number
-
#! /bin/bash a=10 #You cannot type a = 10 so that a will be recognized as a command b=20 if [ $a -eq $b ] #It cannot be typed as [$a -eq $b], and a space is required, otherwise there is a syntax error then echo "$a -eq $b a be equal to b" else echo "$a -eq $b a Not equal to b" fi if [ $a -ne $b ] then echo "$a -ne $b a Not equal to b" else echo "$a -ne $b a be equal to b" fi if [ $a -gt $b ] then echo "$a -gt $b a greater than b" else echo "$a -gt $b a less than b" fi if [ $a -lt $b ] then echo "$a -lt $b a less than b" else echo "$a -lt $b a greater than b" fi if [ $a -ge $b ] then echo "$a -ge $b a Greater than or equal to b" else echo "$a -ge $b a less than b" fi if [ $a -le $b ] then echo "$a -le $b a Less than or equal to b" else echo "$a -le $b a greater than b" fi
-
Boolean operator
-
#! /bin/bash a=10 b=20 if [ $a != $b ] then echo "$a != $b: a Not equal to b" else echo "$a == $b: a be equal to b" fi if [ $a -lt 100 -a $b -gt 15 ] then echo "$a Less than 100 and $b Greater than 15: Return true" else echo "$a Less than 100 and $b Greater than 15: Return false" fi if [ $a -lt 100 -o $b -gt 100 ] then echo "$a Less than 100 or $b Greater than 100: Return true" else echo "$a Less than 100 or $b Greater than 100: Return false" fi if [ $a -lt 5 -o $b -gt 100 ] then echo "$a Less than 5 or $b Greater than 100: Return true" else echo "$a Less than 5 or $b Greater than 100: Return false" fi
-
-
Logical operator
-
#! /bin/bash a=10 b=20 if [[ $a -lt 100 && $b -gt 100 ]] then echo "return true" else echo "return false" fi if [[ $a -lt 100 || $b -gt 100 ]] then echo "return true" else echo "return false" fi
-
-
String operator
-
#! /bin/bash a="abc" b="efg" if [ $a = $b ] then echo "$a = $b : a be equal to b" else echo "$a = $b : a Not equal to b" fi if [ $a != $b ] then echo "$a != $b : a Not equal to b" else echo "$a != $b : a be equal to b" fi if [ -z $a ] then echo "-z $a : String length is 0" else echo "-z $a : The length of the string is not 0" fi if [ -n $a ] then echo "-n $a : String length is not 0" else echo "-n $a : String length is 0" fi if [ $a ] then echo "$a : String is not empty" else echo "$a : The string is empty" fi
-
-
File test operator
-
#! /bin/bash file="/root/test.sh" if [ -r $file ] then echo "File readable" else echo "File unreadable" fi if [ -w $file ] then echo "File writable" else echo "The file is not writable" fi if [ -x $file ] then echo "File executable" else echo "File not executable" fi if [ -f $file ] then echo "The file is ordinary" else echo "The document is a special document" fi if [ -d $file ] then echo "The file is a directory" else echo "File is not a directory" fi if [ -s $file ] then echo "File is not empty" else echo "File is empty" fi if [ -e $file ] then echo "File exists" else echo "file does not exist" fi
-
echo print data
- The echo instruction of Shell is similar to the echo instruction of PHP and is used for string output
-
#! /bin/bash ## Display normal string echo "Hello world" ## Show escape characters echo "\"Hello world\"" ## Display variables name="xyx" echo "$name Hello world" ## Show wrap echo -e "OK! \n" echo "Hello world" ## Show no line breaks echo -e "OK! \c" echo "Hello world" ## Display results directed to file echo "Hello world" > myfile ## Output string as is echo '$name\"' ## Display command execution results echo `date`
test command
- The test command in the Shell is used to check whether a condition is true. It can test values, characters and files
-
number
-
parameter explain -eq Equal to true -ne Not equal to true -gt Greater than is true -ge Greater than or equal to is true -lt Less than is true -le True if less than or equal to
-
-
character string
-
parameter explain = Equal to true != True if not equal -z string If the length of the string is 0, it is true -String n If the length of the string is not 0, it is true
-
-
File test
-
parameter explain -e file name True if the file exists -r file name True if the file exists and is readable -w file name True if the file exists and is writable -x file name True if the file exists and is executable -s file name If the file exists and at least one character is true -f file name True if the file exists and is a normal file -c file name True if the file exists and is a character type special file -b file name True if the file exists and is a block special file -d file name True if the file exists and is a directory
-
-
#! /bin/bash #compare num1=100 num2=100 if test $[num1] -eq $[num2] then echo "The two numbers are equal!" else echo "The two numbers are not equal!" fi
Shell process control
-
if
-
if condition1 then command1 elif condition2 then command2 else commandN fi
-
-
case
- Shell case statement is a multiple selection statement. You can use case statement to match a value with a pattern. If the matching is successful, execute the matching command
-
case value in Mode 1) command1 command2 ... commandn ;; Mode 2) command1 command2 ... commandn ;; esac
-
#! /bin/bash echo "Enter a number from 1 to 4:" echo "The number you entered is:" read num case $num in 1) echo "You chose 1" ;; 2) echo "You chose 2" ;; 3) echo "You chose 3" ;; 4) echo "You chose 4" ;; *) echo "You didn't enter a number between 1 and 4 ;; esac
-
for
- When the variable value is in the list, the for loop executes all commands once and uses the list name to obtain the current value in the list
- The command can be any valid shell command and statement. The in list can contain substitutions, strings, and file names
- The in list is readable. If it is not used, the for loop uses the parameters of the command line
-
for var in item1 item2 ... itemN do command1 command2 ... commandN done
-
#! /bin/bash for loop in 1 2 3 4 5 do echo "The value is: $loop" done for str in "This is a string" do echo $str done
-
while
- The while loop is used to continuously execute a series of commands and read data from the input file; Commands are usually test conditions
-
while condition do command done #Infinite loop while true do command done
-
#Bash let command, which is used to execute one or more expressions. There is no need to add $to represent variables in variable calculation #! /bin/bash i=1 while(($i<=5)) do echo $i let "i++" done
-
break
- The break command allows you to jump out of all loops (terminate the execution of subsequent loops)
-
#! /bin/bash while : do echo -n "Enter a number between 1 and 5" read aNum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ;; *) echo "The number you entered is not between 1 and 5! game over" break ;; esac done
-
continue
- The continue command does not jump out of all loops, only the current loop
-
#! /bin/bash while : do echo -n "Enter a number between 1 and 5" read aNum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ;; *) echo "The number you entered is not between 1 and 5!" continue echo "game over" ;; esac done
Shell function
- The Linux shell can user-defined functions, which can then be called freely in the shell script
- It can be defined with function fun() or directly without any parameters
- When the parameter returns, you can display plus; Return: if not added, the result of the last command will be used as the return value, followed by the value n(0-255)
-
#! /bin/bash ## First function---------------------------------------- demoFun(){ echo "This is my first shell Function!" } echo "------------Function starts execution------------" demoFun echo "------------Function execution completed------------" ## Function return value--------------------------------------- funWithReturn(){ echo "This function adds the two input numbers..." echo "Enter the first number:" read num1 echo "Enter the second number" read num2 echo "The two numbers are $num1 and $num2!" return $(($num1+$num2)) } funWithReturn #The return value of the function is passed through $? To get echo "The sum of the two numbers entered is $? !" ## Function parameters----------------------------------------- funWithParam(){ echo "The first parameter is $1" echo "The second parameter is $2" echo "The third parameter is $3" echo "The tenth parameter is $10" echo "The tenth parameter is ${10}" echo "The eleventh parameter is ${11}" echo "Total parameters $#One“ echo "Output all parameters as a string $* " } funWithParam 1 2 3 4 5 6 7 8 9 10 11 12 13 14