1.shell
Shell command line interpreter, Linux scripting language
1.1 variables
- Common system variables: $HOME $PWD $SHELL $USER
- Strict space rules
- There must be no spaces on either side of the equal sign
- There are spaces in the variable. You can wrap it with "" or ()
- There must be a space between expr operators
- There should be spaces on the left and right sides of conditional judgment []
- There should be spaces after if, while and for
- The default types in bash are string types
- export promotes the variable to a global variable
- $0 script name, 1 − 9 ginseng number , 1-9 parameters, 1 − 9 parameters, # get the number of parameters
- ∗ Obtain take place have ginseng number , can straight meet use to f o r Follow ring , use " *Get all parameters, which can be directly used in the for loop“ * get all parameters, which can be directly used in the for loop. If "*", the will be spliced
- @ Obtain take place have ginseng number , turn become column surface , use " @Get all parameters, turn them into a list, and use“ @Get all parameters, turn them into a list, and use "@" to have the same effect
- $? Get the return value of the previous command
1.2 operators
- Numerical operation method: $(()), $[], expr
- Multiplication operator:
stay[]Internal* stay expr Zhongwei\*
1.3 condition judgment
- Value comparison judgment: - lt -le -eq
- File permission judgment: - r -w -x
- File type judgment: - f -e -d
- String comparison judgment:=
- Multi conditional judgment: & & and | or
1.4 process control
- if select statement:
if [ xxx ];then xxx elif [ xxx ];then fi
- case selection statement:
case $var in xxx) xxx ;; xxx) xxx ;; *) xxx ;; easc
- for loop statement:
for ((xxx;xxx;xxx)) do xxx done
for item in x y z do xxx done
- while loop statement:
while [ xxx ] do xxx done
1.5 console input
-
read xxx
-p "prompt"
-t wait time s
1.6 function
- System function
-
basename file name interception
basename /home/aaa/example.txt # out:expample.txt basename /home/aaa/example.txt .txt #out:example
-
dirname file path interception
dirname /home/aaa/example.txt # out:/home/aaa
-
Custom function
function funname(){ xxx } # Since the shell script runs line by line, it must be declared before calling the function # The formal parameter of the function is specified by $n. The function is called in the following way: funname $x $y
1.7 tools
- cut: crop data, default "\ t"
-
-f: Column number, multiple columns are connected with "," and range with "-"
-
-d: Separator
ifconfig eth0 |grep inet|cut -d " " -f 10 # Cut out the local IP address of linux. The version is different and the display is different
- sed: the stream editor processes one line at a time and does not change the content of the source file
-
-Short for e edit, a short option before multiple commands
-
a add
"na xxx" # Means to insert xxx under line n
-
d delete
"/xxx/d" # Indicates the line where xxx is deleted
-
s replace
"s/old/new/g" # Means to replace old with new
-
- awk: text analysis tool, read in line by line, cut with separator, default separator ""
-
-F: specify separator
-
-v: define variables
awk -F "x" '/pattern/ {action}' filename #Find the row that matches the pattern, split it with "x", and execute the action action BEGIN END # Matches the beginning and end of the line -v i=1 # When using variable i later, you don't need to use $i, just use i
- Built in variable
- FILENAME: file name
- NR: line number
- NF: number of columns after cutting each row
ifconfig eth0|grep inet|awk -F " " '{print $2}' # # Like cut, cut out the local IP address of linux. The version is different and the display is different
- Built in variable
- Sort: sort files
- -n: sort number by value size
- -r: reverse sort
- -t: Specifies the separator used for sorting
- -k: Specifies the column to sort
sort -t 'x' -nk y filename # Split by x, sort by y column
1.8 question bank
-
Query the line number of the empty line in file.txt
awk '/^$/ {print NR}' file.txt
-
Calculate the sum of the second column of file.txt and output [format of each row: name score]
cat file.txt |awk -F " " '{sum+=$2} END{print sum}'
-
shell checks if the file exists
if [ -f file.txt ];then echo "exists" else echo "not exsits" fi
-
Sort digital files
sort -n file.txt
-
Find all the text in the / home folder. The text content contains the keyword "xxx"
grep -r "xxx" /home |cut -d ":" -f 1