Receiving user parameters
#./example.sh One and three
The Shell scripting language includes variables for receiving user parameters, and based on the above command, the commonly used variables are as follows:
- Use $ A To get the file name of the shell script, the above example.sh
- Use $, $, $ ... corresponding to the N-position parameters, such as the One
- With $ #对应着该脚本执行时带有的参数个数, the above $ #等于3
- The $* corresponds to the parameters of all positions, the above $ #为one and the three
- With $? Represents the execution return value of the last command, 0 indicates success, and a value other than 0 indicates a failure
<br/> Determine the user's parameters
- File Test statement
- Logical Test Statement
- Integer value comparison statement
- string comparison statements
Note: Therefore, the syntax format of the test statement is: [conditional expression], the conditional expression has a space before and after each, and there must be a space between the operand and the operator in the conditional expression!!!
<br/>
Common parameters for file testing
operator |
function |
-D |
Test whether the file is a directory type |
-E |
Test whether the file exists |
-F |
Determine if it is a generic file |
-R |
Tests whether the current user has permission to read |
-W |
Tests whether the current user has permission to write |
-X |
Tests whether the current user has permission to execute |
eg. #[-d/etc/fstab]
<br/>
Logical Test Operators
operator |
function |
&& |
With operator, true on the left to calculate right |
| | |
Or operator, false on the left to calculate the right |
! |
Non-operator, true and false inverse |
eg. #[-e/dev/cdrom] && echo "Exist"
<br/>
Integer comparison Operators
operator |
function |
-eq |
is equal to |
-ne |
is not equal to |
-gt |
is greater than |
-lt |
is less than |
-le |
is less than or equal to |
-ge |
is greater than or equal to |
eg. #[10-ge 10]
<br/>
string comparison operators
operator |
function |
= |
Compare strings for the same content |
! = |
Compare strings for different content |
-Z |
Determines whether the string contents are empty or undefined |
eg. #[-Z $String]
<br/>
Process Control Statements
- If statement
- For statement
- While statement
- Case statement
<BR/> If statement syntaxSingle Branch
if 条件测试操作; then命令序列fi
Dual Branch
if 条件测试操作; then命令序列else命令序列fi
Multi-Branch
if 条件测试操作; then命令序列elif 条件测试操作; then命令序列else命令序列fi
<br/>
For statement syntaxfor 变量名 in 取值列表do命令序列done
<br/>
While statement syntaxwhile 条件测试操作do命令序列done
<br/>
Case statement syntaxcase 变量值 in模式1)命令序列;;模式2)命令序列;;......*)默认命令序列esac
Shell Programming Basics