I. Shell's basic syntax-variables
- Naming rules
- Start with a letter
- Connect words with an underscore or an underscore
- The same type is distinguished by numbers
- For file best plus extension
- System variables
- Set and env differences
- Set: Show All variables
- ENV: System variable is environment variable
- Assigning values to variables
- unset VARNAME: Deleting variables
- Common system Variables
- PATH
- Pwd
- Lang
- HOME
- Histsize: Number of history log lines
- PS1: Storing user information
- Ifs
- Export: Defining global variables
- Define the bounds of a variable name
[email protected] ~]# rest_mem=20
[email protected] ~]# echo ${rest_mem}%
20%
- Data type
Two. Shell's basic syntax-operator
- Arithmetic operators
- Relational operations
- <
- >
- <=
- >=
- ==
- !=
- &&
- ||
- Test command related, [] can achieve the same effect
- Assignment operators
- =
- +=
- *=
- /=
- %=
- [email protected] ~]# ((x%=3))
[email protected] ~]# echo $x
- all calculators in the shell
- $[]
- (())
- $ (())
- expr
- floating-point arithmetic b C Software
- yum install bc-y
- echo ' SCALE=2;1/3 ' |bc-l (reserved two bit, 1 divided by 3 operation)
- Test action
- When the command executes, it returns to a system variable of $?
A value of 0 indicates that the command executed successfully or failed.
- Testing command test [] [[]] (())
- Test file status
- -D Directory
- -E
- -H Link File
- -S file length > 0, non-empty
- -F Normal File
- -W Writable
- -R Readable
- -X Executable
- -L Symbolic connection
- -u file has suid bit settings
- -B Block File
- String test
- = Two strings equal
- ! = two strings are not equal
- -Z Empty string
- -N Non-empty string
- Test values
- -eq equals
- -ne Not equal to
- -GT Greater than
- -lt less than
- -ge greater than or equal to
- -le less than or equal to
- Expand Test Symbols
Three. Process Control
The IF statement for Process Control
#!/bin/bashvar='/etc/init.d'#var='/dev/sda'if-d $var ] then "$var is directory"elif-b $var ] then "$var is block"elif-f $var ] then "$var is regular file"else 'unknow'fi
Passing parameters to a script
#test. Shecho $0echo $1echo $2echo $3Echo ${ One}echo'$$'$ $echo'$*'$*Echo' [email protected] '[Email Protected]echo'$#'$#Echo'$?'$?'''test: Python test.sh 1 2 3 4 5 6 7 8 9Output Result:./test.sh2 One$$ 14312$* 1 2 3 4 5 6 7 8 9[email protected] 1 2 3 4 5 6 7 8 9$#$? 0* $ A file name to run* $$ for process PID* $* for all parameters of the input* [email protected] with $** $# number of input parameters* $? Whether the previous command was executed successfully'''
Modify the script so that it can receive the arguments from the caller
[[Email protected]-R3-Srv~]# cat Test_file.sh#!/bin/bashif[-D $1] then Echo"is directory"elif[-B $1] then Echo"is block"elif[-F $1] then Echo"is regular file"ElseEcho' Unknown 'Fi[[email protected]-R3-Srv~]#./TEST_FILE.SH/ETC/PASSWD/etc/passwd isRegularfile
Detects if Nginx is started, and if not, starts it
#! /bin/bashps aux|grep nginx|-v grepif-1 ] then systemctl start nginxfi
Day09_shell Basic grammar and Process Control