1.shell programming overview
1.1 interpretation of shell terms
-
Kernel
- The Linux kernel is mainly to deal with hardware
-
shell
- command interpreter
- Shell is a program written in C language. It is a bridge for users to use Linux. Shell is both a command language and a programming language.
- Shell refers to an application that provides an interface through which users can access the services of the operating system kernel.
-
shell has two main streams:
- sh:
- Bourne shell (SH), Solaris, HPUX default shell
- Bourne again shell (bash), the default shell of Linux system
- csh
- C shell (csh)
- tc shell (tcsh)
- sh:
-
# statement
-
Tell the system that the program specified in the subsequent path is both a Shell program that interprets the script file
-
#! /bin/bash echo "Hello World!"
-
1.2 method of executing Shell
- Enter the absolute or relative path of the script
- /root/helloworld.sh
- ./helloworld/sh
- The executable must be an executable
- bash or sh + script
- sh helloworld.sh
- When the script has no x permissions, root and the file owner
It can be executed normally in this way
- Add '.' or source before the path of the script
- source helloworld.sh
- difference
- The first and second will create a new bash, and the variables in different bashs cannot be shared
- The third is executed in the same shell
- export: you can pass the variables of the current process to the child process for use
- When configuring a profile in the future, all variables must be preceded by export
2. Introduction to shell Basics
2.1 shell variables
-
When defining a variable, the variable name is not marked with a dollar sign
- Naming can only use English letters, numbers and underscores. The first character cannot start with a number.
- There can be no spaces in the middle. You can use underscore ().
- Punctuation cannot be used.
- Keywords in bash cannot be used. (you can use the help command to view reserved keywords)
-
Type of variable
- local variable
- Local variables are defined in scripts or commands and are only valid in the current shell instance. Programs started by other shells cannot access local variables.
- environment variable
- All programs, including those started by the shell, can access environment variables. Some programs need environment variables to ensure their normal operation.
- shell variable
- Shell variables are special variables set by the shell program. Some shell variables are environment variables and some are local variables
- local variable
-
#Declaration of variables name="zhangsan" for file in `ls /etc` or for file in $(ls /etc) #Call of variable echo $name echo ${name} for skill in Ada Coffe Action Java; do echo "I am good at ${skill}Script" done #Read only variable / bin/sh:NAME: This variable is read only url="https://www.google.com" readonly url url="https://www.baidu.com" #Delete variable unset name
2.2 string of shell
-
String is the most commonly used and useful data type in shell programming. Letter strings can use single quotation marks, double quotation marks or no quotation marks.
-
Single quotation mark
- Any character in the single quotation mark will be output as is, and the variable in the single quotation mark string is invalid;
- A single quotation mark cannot be a single quotation mark in a single quotation mark string, but can appear in pairs and be used as string splicing.
-
Double quotation mark
-
You can have variables in double quotes
-
Escape characters can appear in double quotation marks
-
#Declaration string str1="hello world 1" str2="hello world 2" #String splicing -- double quotes name='helloworld' name1="hello,"$name" !" name2="hello,${name} !" #String splicing -- single quotation mark passwd='123456' passwd1='hello,'$passwd' !' passwd2='hello,${passwd} !' echo $passwd2 #hello,${passwd} ! #Length of string email="123456@qq.com" echo ${#email} echo ${email:1:4} #Output characters from positions 1 to 4, 2345
-
2.3 shell array
-
bash supports one-dimensional arrays (multi-dimensional arrays are not supported) and does not limit the size of the array
-
The subscripts of array elements are numbered from 0. The subscript is used to obtain the elements in the array. The subscript can be an integer or an arithmetic expression, and its value should be greater than or equal to 0
-
#Define array parentheses to represent the array, and the array elements are separated by a space symbol Array name=(Value 1 value 2 ... value n) favs=("Football" "Basketball" "Table Tennis" "bowling") #Read array ${array name [subscript]} fav=${favs[1]} #Use the @ symbol to get all the elements in the array echo ${favs[@]} #Gets the length of the array length=${#favs[@]} length=${#favs[*]}
2.4 shell comments
-
Lines beginning with # are comments and are ignored by the interpreter.
-
Set multiline comments by adding a # sign to each line
-
#---------------------------- # This is a comment # author: # site: #---------------------------- #### Server configuration-start #### # # # # #### Server configuration-end #### # Special multiline comments :<<EOF Note Content ... Note Content ... Note Content ... EOF :<<! Note Content ... Note Content ... Note Content ... !
2.5 shell parameter transfer
-
When executing a shell script, pass parameters to the script. The format of parameters obtained in the script is: $n (n represents a number)
-
Parameter processing Parameter description $# Number of parameters passed to the script $* Displays all parameters passed to the script in a single character. $$ ID number of the current process the script is running $! ID number of the last process running in the background $? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error. $0 File name of the execution -
#! /bin/bash echo "shell Pass parameter instance!"; echo "Executed file name: $0"; echo "The first parameter is: $1"; echo "The second parameter is: $2"; echo "The third parameter is: $3"; # ./hello.sh 11 22 33 44
3. Advanced shell
3.1 shell operators
-
Classification of operators
-
Arithmetic operator
-
operator explain give an example + addition expr $a + $b results in 30. - subtraction expr $a - $b results in - 10. * multiplication expr $a \* $b results in 200. / division expr $b / $a results in 2. % Surplus Expr $B% $a results in 0. = assignment a=$b assigns the value of variable b to a. == equal. It is used to compare two numbers. If they are the same, it returns true. [$a == $b] returns false. != Unequal. It is used to compare two numbers. If they are different, it returns true. [$a! = $b] returns true. **Note: * * conditional expressions should be placed between square brackets with spaces, for example:[ a = = a== a==b] is wrong and must be written as [$a == $b].
-
Relational operator
-
operator explain give an example -eq Check whether two numbers are equal, and return true if they are equal. [$a -eq $b] returns false. -ne Check whether the two numbers are not equal, and return true if they are not equal. [$a -ne $b] returns true. -gt Check whether the number on the left is greater than that on the right. If so, return true. [$a -gt $b] returns false. -lt Check whether the number on the left is less than that on the right. If so, return true. [$a -lt $b] returns true. -ge Check whether the number on the left is greater than or equal to that on the right. If so, return true. [$a -ge $b] returns false. -le Check whether the number on the left is less than or equal to that on the right. If so, return true. [$a -le $b] returns true. Relational operators only support numbers, not strings, unless the value of the string is a number.
-
Boolean operator
-
operator explain give an example ! If the expression is true, it returns false; otherwise, it returns true. [! false] returns true. -o Or operation. If an expression is true, it returns true. [$a -lt 20 -o $b -gt 100] returns true. -a And operation, both expressions return true only when they are true. [$a -lt 20 -a $b -gt 100] returns false. -
Logical operator
-
operator explain give an example && Logical AND [[$a - LT 100 & & $B - GT 100]] returns false || Logical OR [[$a - LT 100 | $B - GT 100]] returns true -
String operator
-
operator explain give an example = Check whether two strings are equal, and return true if they are equal. [$a = $b] returns false. != Detect whether two strings are not equal, and return true if they are not equal. [$a! = $b] returns true. -z Check whether the string length is 0, and return true if it is 0. [- z $a] returns false. -n Check whether the string length is not 0, and return true if it is not 0. [- n "$a"] returns true. $ Check whether the string is empty. If not, return true. [$a] returns true. -
File test operator: used to detect various attributes of Unix files.
-
Operator explain give an example -b file Check whether the file is a block device file. If so, return true. [- b $file] returns false. -c file Check whether the file is a character device file. If so, return true. [- c $file] returns false. -d file Check whether the file is a directory. If so, return true. [- d $file] returns false. -f file Check whether the file is an ordinary file (neither a directory nor a device file). If so, return true. [- f $file] returns true. -g file Check whether the SGID bit is set in the file. If so, return true. [- g $file] returns false. -k file Check whether the file has a sticky bit set. If so, return true. [- k $file] returns false. -p file Check whether the file is a famous pipeline. If so, return true. [- p $file] returns false. -u file Check whether the SUID bit is set in the file. If so, return true. [- u $file] returns false. -r file Check whether the file is readable. If so, return true. [- r $file] returns true. -w file Check whether the file is writable. If so, return true. [- w $file] returns true. -x file Check whether the file is executable. If so, return true. [- x $file] returns true. -s file Check whether the file is empty (whether the file size is greater than 0). If it is not empty, return true. [- s $file] returns true. -e file Check whether files (including directories) exist. If so, return true. [- e $file] returns true. Other checkers:
- -S: Determine whether a file is a socket.
- -50: Detects whether the file exists and is a symbolic link.
-
3.1.1 arithmetic operators
-
expr is an expression evaluation tool, which can be used to complete the operation of expression evaluation.
-
#!/bin/bash a=10 b=20 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 $b / $a` echo "b / a : $val" val=`expr $b % $a` echo "b % a : $val" if [ $a == $b ] then echo "a be equal to b" fi if [ $a != $b ] then echo "a Not equal to b" fi
-
a + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a Not equal to b
3.1.2 relational operators
-
Relational operators only support numbers, not strings, unless the value of the string is a number.
-
#!/bin/bash a=10 b=20 if [ $a -eq $b ] 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 Not greater than b" fi if [ $a -lt $b ] then echo "$a -lt $b: a less than b" else echo "$a -lt $b: a Not less 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
-
10 -eq 20: a Not equal to b 10 -ne 20: a Not equal to b 10 -gt 20: a Not greater than b 10 -lt 20: a less than b 10 -ge 20: a less than b 10 -le 20: a Less than or equal to b
3.1.3 Boolean operators
-
#!/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
-
10 != 20 : a Not equal to b 10 Less than 100 and 20 more than 15 : return true 10 Less than 100 or more than 20 : return true 10 Less than 5 or 20 more than 100 : return false
3.1.4 logical operators
-
#!/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
-
return false return true
3.1.5 string operators
-
#!/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 : The string length is 0" else echo "-z $a : String length is not 0" fi if [ -n "$a" ] then echo "-n $a : String length is not 0" else echo "-n $a : The string length is 0" fi if [ $a ] then echo "$a : String is not empty" else echo "$a : The string is empty" fi
-
abc = efg: a Not equal to b abc != efg : a Not equal to b -z abc : String length is not 0 -n abc : String length is not 0 abc : String is not empty
3.1.6 document test operator
-
#!/bin/bash #The variable file represents the file / var/www/runoob/test.sh, which is 100 bytes in size and has rwx permissions. file="/var/www/runoob/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 "The file is not executable" fi if [ -f $file ] then echo "The file is a normal file" else echo "The file is a special file" 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 "The file is empty" fi if [ -e $file ] then echo "File exists" else echo "file does not exist" fi
-
File readable File writable File executable The file is a normal file File is not a directory File is not empty File exists
3.2 echo print data
-
The echo instruction of the shell is similar to the echo instruction of PHP and is used for string output.
-
## Display normal string echo "Hello World" ## Show escape characters echo "\"Hello World\"" ## Display variables name="zhangsan" 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`
3.3test command
-
The test command in the Shell is used to check whether a condition is true. It can test values, characters and files.
-
Numerical test
-
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
-
-
String test
-
parameter explain = Equal to true != True if not equal -z string True if the length of the string is zero -n string True if the length of the string is not zero
-
-
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 True if the file exists and has at least one character -d file name True if the file exists and is a directory -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
-
-
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
3.4 shell process control
3.4.1 if
-
if
-
if condition then command1 command2 ... commandN fi
-
-
if else
-
if condition then command1 command2 ... commandN else command fi
-
-
if else-if else
-
if condition1 then command1 elif condition2 then command2 else commandN fi
-
-
example
-
a=10 b=20 if [ $a == $b ] then echo "a be equal to b" elif [ $a -gt $b ] then echo "a greater than b" elif [ $a -lt $b ] then echo "a less than b" else echo "No conditions met" fi
-
a less than b
-
3.4.2 case ... esac
-
Case... esac is a multi choice statement, which is a multi branch selection structure. Each case branch starts with a right parenthesis and two semicolons;; It means break, that is, the execution ends, the whole case... esac statement jumps out, and esac (that is, the reverse of case) is used as the end mark
-
You can use a case statement to match a value with a pattern. If the match is successful, execute the matching command.
-
Syntax format:
-
case value in Mode 1) command1 command2 ... commandN ;; Mode 2) command1 command2 ... commandN ;; esac
-
-
example
-
echo 'Enter a number between 1 and 4:' echo 'The number you entered is:' read aNum case $aNum in 1) echo 'You chose 1' ;; 2) echo 'You chose 2' ;; 3) echo 'You chose 3' ;; 4) echo 'You chose 4' ;; *) echo 'You did not enter a number between 1 and 4' ;; esac
-
Enter a number between 1 and 4: The number you entered is: 3 You chose 3
-
3.4.3 for
-
When the variable value is in the list, the for loop executes all commands once and uses the variable 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 optional. If it is not used, the for loop uses the positional parameter of the command line
-
Format:
-
for var in item1 item2 ... itemN do command1 command2 ... commandN done #Write in one line for var in item1 item2 ... itemN; do command1; command2... done;
-
-
example
-
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
-
The value is: 1 The value is: 2 The value is: 3 The value is: 4 The value is: 5 This is a string
-
3.4.4 while loop
-
The while loop is used to continuously execute a series of commands and to read data from the input file. Commands are usually test conditions.
-
while condition do command done
-
# Bash let command, which is used to execute one or more expressions. It is not necessary to add $to represent variables in variable calculation #!/bin/bash int=1 while(( $int<=5 )) do echo $int let "int++" done
-
Infinite loop
-
while : do command done #perhaps while true do command done #perhaps for (( ; ; ))
-
3.4.5 until cycle
-
The until loop executes a series of commands until the condition is true.
-
The until loop is handled in the opposite way to the while loop.
-
until condition do command done
-
condition is generally a conditional expression. If the return value is false, continue to execute the statements in the loop body, otherwise jump out of the loop.
3.4.6 break
-
The break command allows you to jump out of all loops (terminate the execution of all 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
-
Enter a number between 1 and 5:3 The number you entered is 3! Enter a number between 1 and 5:7 The number you entered is not between 1 and 5! game over
3.4.7 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
-
When a number greater than 5 is entered, the loop in this example will not end, and the echo "game end" statement will never be executed.
3.5 shell functions
-
linux shell can user-defined functions, which can be called freely in shell scripts.
-
It can be defined with function fun() or directly with fun() without any parameters.
-
Parameter returns, plus can be displayed: return returns. If not, the result of the last command will be used as the return value. Return followed by the value n(0-255)
-
The format is as follows:
-
[ function ] funname [()] { action; [return int;] }
-
-
example
-
#!/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 aNum echo "Enter the second number: " read anotherNum echo "The two numbers are $aNum and $anotherNum !" return $(($aNum+$anotherNum)) } funWithReturn # The return value of the function is passed through $? To get echo "The sum of the two numbers entered is $? !" ## Function parameters---------------------- #In Shell, when you call a function, you can pass parameters to it. Inside the function body, the value of the parameter is obtained in the form of $n funWithParam(){ echo "The first parameter is $1 !" echo "The second parameter is $2 !" echo "The tenth parameter is $10 !" echo "The tenth parameter is ${10} !" echo "The eleventh parameter is ${11} !" echo "The total number of parameters is $# One! " echo "Output all parameters as a string $* !" } funWithParam 1 2 3 4 5 6 7 8 9 34 73
-
## First function------------------- -----Function starts execution----- This is my first shell function! -----Function execution completed----- ## Function return value----------------- This function adds the two input numbers... Enter the first number: 1 Enter the second number: 2 The two numbers are 1 and 2, respectively ! The sum of the two numbers entered is 3 ! ## Function parameters---------------------- The first parameter is 1 ! The second parameter is 2 ! The tenth parameter is 10 ! #This is actually the first parameter of $1 plus the character 0 The tenth parameter is 34 ! The eleventh parameter is 73 ! There are 11 parameters in total! Output all parameters as a string 1 2 3 4 5 6 7 8 9 34 73 !
-
-
be careful, 10 no can Obtain take The first ten individual ginseng number , Obtain take The first ten individual ginseng number need want 10 the tenth parameter cannot be obtained. It is required to obtain the tenth parameter 10 cannot get the tenth parameter. Getting the tenth parameter requires {10}. When n > = 10, ${n} needs to be used to get the parameter.
-
There are also several special character processing parameters:
-
Parameter processing explain $# The number of arguments passed to the script or function $* Displays all parameters passed to the script in a single string $$ ID number of the current process the script is running $! ID number of the last process running in the background $@ Same as $*, but use in quotation marks and return each parameter in quotation marks. $- Displays the current options used by the Shell, with the same function as the set command. $? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.
-