Shell learning notes and shell scripts
I. variable naming
The variable name must start with a letter or underline and consist of letters, numbers, or underscores. The variable name length cannot exceed 255 characters.
Ii. Classification of Variables
1. User-Defined variables
2. Environment Variables
3. Location Parameter Variables
4. predefined Variables
3. User-Defined variables
The user-defined variable is a local variable and can only take effect in the current Shell.
1. Define Variables
Variable name = variable value
Example:
Note: there must be no spaces before and after the equal sign. Otherwise, the following error message will appear:
2. Variable call
$ Variable name or $ {variable name}
Example:
3. Variable superposition
Example:
Or:
4. delete variables
Unset variable name
Example:
5. View Variables
1) env: view all environment variables
2) set: view all variables (including User-Defined variables)
Iv. Environment Variables
The environment variable is a global variable, which takes effect in the current Shell and all the sub-shells of this Shell.
1. Set Environment Variables
Export variable name = variable value
Or
Variable name = variable value
Export variable name
Example:
Note: When setting environment variables, we recommend that you use uppercase letters to avoid conflicts with User-Defined variables or system commands (both in lower case.
2. Delete environment variables, call environment variables, and view environment variables
User-Defined variables are used in the same way.
3. Set the PS1 environment variable
PS1 environment variables will affect user prompt information, for example:
5. location variable 1. $ n
N is a number, $0 represents the command itself, $1-$9 represents the first to ninth parameters, more than 10 parameters need to be included in braces, such as ${10 }.
Example:
#!/bin/basha=$1b=$2sum=$(($a +$b))echo $sum
2. $ *
This variable represents all parameters in the command line. $ * considers all parameters as a whole.
#! /Bin/bashfor I in "$ *" do echo $ idone # Run: test. sh 1 2 3 # output: 1 2 3 # Explanation: All parameters in $ * are regarded as a whole, so this for loop only repeats once.
3. $ @
This variable also represents all the parameters in the command line, but $ @ treats each parameter differently.
#!/bin/bashfor i in "$@"do echo $idone
# Run: test. sh 1 2 3 # output:
1 2 3 # Explanation: every parameter in $ @ is regarded as independent, so this for loop will be repeated multiple times.
4. $ #
This variable represents the number of all parameters in the command line.
#! /Bin/bashecho $ # Run: test. sh 1 2 3 # output: 3
6. predefine variable 1. $?
The return status of the last executed command. If the value is 0, it indicates that the previous command is correctly executed. If the value is not 0, it indicates the error code of the previous command.
Example:
2. $
Current process ID (PID)
Example:
3. $!
PID of the last process running in the background)
#!/bin/bashfind /root -name "test.sh" &echo $!
VII. read command
Accept keyboard input
1. parameter description
-P "prompt information": the output prompt information while waiting for the read input
-T seconds: The read command waits for user input. You can use this parameter to specify the waiting time.
-N characters: If the read command only accepts the specified number of characters, it will continue to be executed.
-S: hides input data, which is applicable to input of confidential information.
2. Example
#!/bin/bashread -p "please input username:" usernameread -p "please input password:" -s passwordecho -e "\n"read -p "please input sex[F/M]:" -n 1 sexecho -e "\n"echo "Welcome $username[$sex]"