Fun Bash Script: select the structure if
3rd articles
Almost all programming languages have the concept of process control, that is, order structure, selection structure and loop structure.
The selected structure is also called the branch structure, such asIfAndSwitchStatement.
If condition
I have already discussed the use of the test Expression and Its Simplified [] Operator. These judgment statements can be used as conditions for the if structure.
In Bash shell, any command (whether internal or external) that can have the true or false judgment function can be used as a condition for judgment.
Basic Structure
In Bash, if also supports the else and else if pairs, except that else if is written here as elif.
The basic structure of the if statement is as follows:
If <condition 1> then Statement 1 elif <condition 2> Statement 2 elif <condition...> statement... else statement nfi
Note that if statements have no angle brackets <>. Replace <condition> with your conditional expression.
Use elif and else as needed. Bash is different from the C language. It does not use curly brackets to close a scope. Bash's if statement closed scope uses if's ""-fi.
This is an interesting phenomenon, and we will see similar writing later.
For then, the if condition must be followed by a reserved word then. You can also write them in the same line, but note that they should be separated by semicolons. These bash interpreters know that then is not part of the if condition. Otherwise, an error is reported.
If <condition>; then
Example of Writing test and [] conditions
Enter a number and determine whether it is less than 100
#! /Bin/bashread-p "enter a number:" aif [$ a-lt 100] then echo "a <100" else echo "a> = 100" fi
Determine whether the next file in the current directory exists. If yes, determine whether the file is a directory file.
#! /Bin/bashread-p "enter a file name:" nameif [-e $ name] then echo-n "$ name exists, "if [-d $ name] then echo" and is a directory "else echo" but not a directory "fielse echo" $ name does not exist "fi
Other commands as conditions
Determine whether a command is installed in the current environment.
#! /Bin/bashread-p "enter a command:" varif which $ var>/dev/nullthen echo "$ var" else echo "$ var does not exist" fi
This script is named var. sh. Please refer to its execution results.
Pay attention to the following points. In C, 0 is false, and not 0 is true. In Bash, the opposite is true.
Run the which pwd command on the terminal. Then use echo $? Check the execution status of the command (or the returned value ).
Echo $? The result is 0. The which delete command is executed, and then echo $? The output is 1.
Of course, in addition to which, there are many internal or external commands that can be used in combination with the if structure. seamless integration with external commands is the powerful foundation of Bash and other shells.
If and logical expressions
Like other languages, if in Bash can also be used in combination with or not. In addition, it can be used together with internal or external commands to achieve the effect that other languages cannot achieve.
For example:
If [-e $ filename] & rm $ filenamet=echo "$ filaname does not exist! "Fi