The third part of Linuxshell script is about shell commands and process control. since we have not been able to talk about process control in space, today we will only introduce the ifcase and select parts of the previous three sections. The following three parts can only be introduced in section 4 of Linuxshell script basics. Linux shell script basic learning Lecture 3: When we introduced shell commands and process control in the previous section, we could not talk about process control due to space limitations, today, we will only introduce the first three parts of if case and select. The following three parts can only be introduced in the fourth lecture of Linux shell script basics.
1.1.5 Shell command and process control (2)
3) process control
1. if
If the expression "if" is true, the part after then is executed:
if ....; then....elif ....; then....else....fi
In most cases, you can use test commands to test the conditions. For example, you can compare strings, determine whether a file exists, and whether the file is readable...
"[]" Is usually used to represent a conditional test. Note that spaces are important. Make sure that the square brackets have spaces.
[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.
[-N "$ var"]: determines whether the $ var variable has a value.
["$ A" = "$ B"]: determines whether $ a and $ B are equal.
Run man test to view all types of test expressions that can be compared and judged.
Directly execute the following script:
#!/bin/shif [ "$SHELL" = "/bin/bash" ]; thenecho "your login shell is the bash (bourne again shell)"elseecho "your login shell is not bash but $SHELL"fi
The variable $ SHELL contains the name of the logon shell. we compared it with/bin/bash.
Shortcut operators
If you are familiar with the C language, you may like the following expressions:
[ -f "/etc/shadow" ] && echo "This computer uses shadow passwors"
Here & is a shortcut operator. if the expression on the left is true, execute the statement on the right.
You can also think of it as a logical operation. In the above example, if the/etc/shadow file exists, print "This computer uses shadow passwors ". Similarly, operations (|) are also available in shell programming. Here is an example:
#!/bin/shmailfolder=/var/spool/mail/james[ -r "$mailfolder" ]' '{ echo "Can not read $mailfolder" ; exit 1; }echo "$mailfolder has mail from:"grep "^From " $mailfolder
The script first checks whether mailfolder is readable. If it is readable, the "From" line in the file is printed. If it is not readable or the operation takes effect, print the error message and exit the script. There is a problem here, that is, we must have two commands:
-Print the error message.
-Exit the program.
We use curly braces to put the two commands together as one command in the form of an anonymous function. General functions will be mentioned below.
We can use the if expression to do anything without the sum or operator, but it is much more convenient to use the sum or operator.
2. case
Case: The expression can be used to match a given string, not a number.
case ... in...) do something here ;;esac
Let's look at an example. The file command can identify the file type of a given file, for example:
File lf.gz
This will return:
lf.gz: gzip compressed data, deflated, original filename,last modified: Mon Aug 27 23:09:18 2001, os: Unix
We use this to write a script named smartzip, which can automatically decompress bzip2, gzip, and zip compressed files:
#!/bin/shftype=`file "$1"`case "$ftype" in"$1: Zip archive"*)unzip "$1" ;;"$1: gzip compressed"*)gunzip "$1" ;;"$1: bzip2 compressed"*)bunzip2 "$1" ;;*) echo "File $1 can not be uncompressed with smartzip";;esac
You may notice that we use a special variable $1 here. The variable contains the first parameter value passed to the program.
That is, when we run:
smartzip articles.zip
$1 is the string articles.zip
3. selsect
The select expression is a bash extension application, especially for interactive use. You can select from a group of different values.
select var in ... ; dobreakdone.... now $var can be used ....
The following is an example:
#!/bin/shecho "What is your favourite OS?"select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; dobreakdoneecho "You have selected $var"
The following is the result of running the script:
What is your favourite OS?1) Linux2) Gnu Hurd3) Free BSD4) Other#? 1You have selected Linux
The above is a detailed introduction to basic learning of Linux shell scripts (III). For more information, see PHP Chinese website (www.php1.cn )!