1. Shell Script Parameters
- The $n represents the nth parameter passed to the script, for example, $ $ = 1th argument, and $ = 2nd parameter ....
- $# number of command-line arguments
- The name of the current script
- $* returns the values of all parameters in the form "parameter 1, parameter 2, Parameter 3 ..."
- [Email protected] with "parameter 1" "Parameter 2" "Parameter 3" ... Returns the value of all parameters in the form
- $_ last parameter of the command executed before saving
- $? The return status code of the previous command or function
- $$ returns the process ID (PID) of this program
Note: variables [email protected] can be "parameter 1" "Parameter 2" "Parameter 3". The form returns all parameter values, therefore, [email protected] and "$" "$ $" ... is equivalent, if the user passes a parameter that contains spaces or other special characters, you need to use [email protected] to get all the values of the parameters, and you cannot use $*.
2. Variables and references
- In the shell, the case of variable names is sensitive, so the two variable names that differ in capitalization do not represent the same variable.
- M=${X/ACD/1}---To change the ACD in X to 1 and assign it to M.
# x=acdqw12# m=${x/acd/1echo $m #m=1qw12
- 3 kinds of quotation marks are different: the single quotation mark (") is enclosed in a character nonalphanumeric as a normal character-full reference; the characters enclosed in double quotation marks (" ") except for" $ "" \ "" "" "" "these characters still retain their special functions, the remaining characters are treated as ordinary characters-part references; The string is interpreted by the shell as a command, and at execution time, the shell executes the command first and replaces the entire anti-quote part with its standard output.
- The backslash "\" is called an escape character and is used to convert special characters in some shells to ordinary characters, such as "$" or "'".
echo/bin/echo \ $SHELL # $SHELL.
- By convention, environment variables in the shell are all capitalized.
- Command substitution: The inverse quotation marks and the parentheses are equivalent;
' Shell_command ' ==$ (Shell_command)
- Variable assignment Syntax: variable_name=value;variable_name represents the variable name; value represents the values that will be assigned to the variable; the middle equals "=" is called an assignment symbol, and the left and right sides of the assignment symbol cannot be directly followed by a space, or the shell treats it as a command ; The correct assignment statement:
# v1=linux# v2='redhet linux'# v3="Redhat Linux $HOSTTYPE "# v4=12345.
- To clear a variable:
Unset variable_name
3. Condition Testing
- There are two types of conditional test syntax: test and [; test expression;[expression]; expression and left and right square brackets must also have a space between them.
- The left and right sides of the symbol for the test operator =,! =, and z must contain a space.
- Integer test Syntax:
Test Number1 op number2 or [Number1 op number2]
OP expression operator:-eq (equals),-ne (Not Equal),-GT (greater than),-lt (less than),-ge (greater than or equal),-le (less than equals); [12-gt]-->echo $?] 1.
- File test Syntax:
file ]
The-a file is present, the-B file is present and is a block file, the-c file is present and is a character file, and the-D file exists and is the directory;-e file is the same as-A; the-s file is longer than 0 or the file is not empty;-F fil e files exist and are regular files; the-w file file exists and is writable; the-x file file exists and is executable; the-r file is present and readable; the-u file file does not have a suid bit, and the-l file is present and symbolic.
- Logical operators: String tests, Integer tests, and files all provide a test condition, whereas in shell programming, it is often encountered that multiple conditions are judged simultaneously, and logical operators can combine many different conditions to form a complex conditional expression;
! -a expression2 logic with expression1 o expression2 logic or
- ":" The colon is a special command that refers to an empty command, which does nothing, but its exit status is always 0, and if we change the command as a condition in the IF statement, then the then clause will be executed forever.
- Conditional Judgment statement: if
if expression Then statement1 statement2 ... fi
Or
If Expression;then
Statement1
Statement2
Fi
If Else
#!/bin/BashEcho "Please enter a score"#输入提示信息read Score #读取用户输入数据 # If the user does not enter a number, prompt the user to re-enterif[-Z"$score"]; Then#-Z is an empty stringEcho "You enter nothing. Please enter a score:"Read ScoreElse#如果用户输入的数据不对, re-enterif["$score"-lt0-O"$score"-gt -]; Then Echo "The score should be between 0 and 100.Please enter again:"Read ScoreElse#输出Aif["$score"-ge -]; Then Echo "The grade is A." Else#输出Bif["$score"-ge the]; Then Echo "The grade is B." Else#输出Cif["$score"-ge -]; Then Echo "The grade is C." Else#输出Dif["$score"-ge -]; Then Echo "The grade is D." Else#输出EEcho "The grade is E." fi fi fi fi fifi
If Elif
#!/bin/BashEcho "Please enter a score"#输入提示信息read Score #读取用户输入数据 # If the user does not enter a number, prompt the user to re-enterif[-Z"$score"]; Then#-Z is an empty stringEcho "You enter nothing. Please enter a score:"Read ScoreElse#如果用户输入的数据不对, re-enterif["$score"-lt0-O"$score"-gt -]; Then Echo "The score should be between 0 and 100.Please enter again:"Read ScoreElse#输出Aif["$score"-ge -]; Then Echo "The grade is A."#输出Belif["$score"-ge the]; Then Echo "The grade is B."#输出Celif["$score"-ge -]; Then Echo "The grade is C."#输出Delif["$score"-ge -]; Then Echo "The grade is D." Else#输出EEcho "The grade is E." fi fifi
Case
Case inch value1) Statement1 statement2 ... STATEMENTN;; value2) statement1 statement2 ... STATEMENTN;; ... valuen) statement1 statement2 ... STATEMENTN;; *) statement1 statement2 ... STATEMENTN;; Esac
In the above syntax, variable is a variable, and the case statement compares the value of the variable to each value in Value1~valuen, and if it is equal to the value of a value, the set of statements corresponding to that value are executed. When encountering ";;" Symbol, it jumps out of the case statement, executes the statement following the ESAC statement, and executes a set of statements after the * If no value matches the value of variable.
Note: For the case statement above, the user should be aware of the following points:
I for variable name variable, can use double quotation marks, also can not use;
II The conditional test part of each case clause ends with a closing parenthesis ")";
III Each CASE clause is a pair of semicolons ";;" As a terminator, during the execution of a script, when a pair of semicolons is encountered, all the case clauses following the current clause clause are skipped, including the corresponding clauses of *, executing the other statements following the ESAC clause.
The IV case statement structure ends with ESAC.
4. Calculation of the expression of an operation
- Expr expression computes the value of an expression when using expr, the operation symbol needs to be transferred, \* \ (\).
#!/bin/Bashresult=`Expr 2- -`Echo "$result"result=`Expr 2\* -`Echo "$result"result=`Expr\(2-6\) \*2`Echo "$result"
- Use $ (()) for arithmetic operations without the need to transfer operators and parentheses.
#!/bin/bashresult=$ ((3+6))echo"$result" result=$ ((3*6))echo"$result "
- Use $[...] with the same usage as $ (())
- You can use the Let command to execute one or more arithmetic expressions where the variable name does not need to use the $ symbol and must be referenced if the expression contains spaces or other special characters.
#!/bin/Bashn=tenletn=n+1echo'$n'let n=n*echo"$n"
5. Bitwise operators
Operator |
Description |
Example |
<< |
Move left |
4<<2, move 4 to the left 2 bits, the result is 16 |
>> |
Move right |
8>>2, move 8 to the right 2 bits, the result is 2 |
& |
Bitwise-AND |
8&&4, the bitwise AND operation of 8 and 4, with a result of 0 |
| |
Bitwise OR |
8|4, 8 and 4 are bitwise OR arithmetic, the result is 12 |
~ |
Bitwise non- |
8 bitwise non-operation with a result of-9 |
^ |
Bitwise XOR OR |
10^6, 10 and 6 are bits by XOR, and the result is 12 |
Update in .....
Shell Learning Notes