Basic shell syntax and shell syntax
I. Variables
1. Naming rules for variables: The name should start with a letter or underline followed by numbers, letters or underscores. It is best not to name it casually, so that you can see the variable name to guess its meaning.
2. Variable assignment: x = 100
Echo $ x
Delete variable: unset x
3. Braces are used to define the boundary of variable names.
[Root @ bogon ~] # Egon_salary = 20000
[Root @ bogon ~] # Echo $ {egon_salary} yuan
20000 yuan
4. The data type does not need to be declared in bash. The default data type is accept.
Ii. Operators
1. Arithmetic Operators: +-*/%
[Root @ bogon ~] # Echo $ [5% 2]
1
2. assignment operator: =, + =,-=, * =,/=, % =
[Root @ bogon ~] # X = 10
[Root @ bogon ~] # (X + = 1 ))
[Root @ bogon ~] # Echo $ x
11
3. Relational operators: <,> ,! =, =, >=, <=, | ,&&
Relational operators are often used with (). [] can achieve the same result, but () cannot determine the type of a file. To determine the file type, you must use [], [] the same effect as the test command
$? View the command execution result. If the result is 0, it indicates true. If the result is not 0, it indicates false.
[Root @ bogon ~] # X = 10
[Root @ bogon ~] # (X> = 8 ))
[Root @ bogon ~] # Echo $?
0
4. Calculator in shell
We have previously said that you can use $ [] to perform some simple operations. However, if the decimal operation is involved, you need to use the calculator in the shell.
First install the software, yum install-y bc
[Root @ bogon ~] # Res = $ (echo 'scale = 2; 1/3 '| bc-l | cut-d'.'-f2)
[Root @ bogon ~] # Echo $ {res} %
33%
5. test the command
Test
-N str String Length cannot be zero
-Z str String Length: 0
-File B exists and is a block File
-D file exists and is a directory file
-E file exists
-F: The file exists and is a common file.
-H file exists and is a linked file (same as-L)
-S file exists and must be greater than zero bytes
Comparison Between Files
File1-nt file2 file1 is created later than file2
File1-ot file2 file1 is created earlier than file2
Comparison between Integers
Int1-ne int2 int1 and int2 are not equal
Int1-eq int2 int1 and int2 are equal
Int1-lt int2 int1 less than int2
Int1-le int2 int1 less than or equal to int2
Int1-gt int2 int1 greater than int2
Int1-ge int2 int1 greater than or equal to int2
String comparison
Str1 = str2 str1 and str2 are equal
Str1! = Str2 str1 and str2 are not equal
Comparison Between Expressions
Expression1-a expression2 expression 1 and expression 2 are both true
Expression1-o expression2 expression 1 or expression 2 is true
6. test example
Digit comparison test:
[root@bogon ~]# [[ 2 > 1 ]][root@bogon ~]# echo $?0[root@bogon ~]# ((20>10))[root@bogon ~]# echo $?0[root@bogon ~]# ((20<10))[root@bogon ~]# echo $?1
String Testing
[root@bogon ~]# [ "abc" = "abc" ][root@bogon ~]# echo $?0[root@bogon ~]# [[ "abc" = "abc" ]][root@bogon ~]# echo $?0[root@bogon ~]# (("abc" = "abc"))[root@bogon ~]# echo $?1
[root@bogon ~]# [[ a = a && 1 < 2 ]][root@bogon ~]# echo $?0[root@bogon ~]# [[ a = a && 1 < 2 ]][root@bogon ~]# echo $?0
[root@bogon ~]# (( a = a || 1 > 2 ))[root@bogon ~]# echo $?1[root@bogon ~]# [[ a = a || 1 > 2 ]][root@bogon ~]# echo $?0
Simply compare numbers, use (())
In addition to comparing numbers, use [[]
3. Process Control
1. if Branch
1) Verify the user account password:
input your name : zhangcaninput password : 123login successful[root@bogon ~]# ./usertest.sh input your name : hhainput password : haguser or password error
#! /bin/bashuser='zhangcan'password='123'read -p 'input your name : ' nameread -p 'input password : ' codeif [ $name = $user -a $code = $password ];then echo 'login successful'else echo 'user or password error'fi~
2) grade the score
#! /Bin/bash # judge the grade based on the score entered by the user, and output it to the User read-p 'input your score: 'scoreif [$ score-ge 90]; then echo 'excellent 'elif [$ score-ge 70-a $ score-lt 90]; then echo 'good elif [$ score-ge 60-a $ score-lt 70]; then echo 'failed' elif [$ score-lt 60]; then echo 'poorer 'fi
2. while Loop
While (condition)
Do
Command
Done
Example: determine the type of file input by the user
#!/bin/bashwhile :do read -p 'input your file : ' file if [ -z $file ];then continue else break fidoneif [ -f $file ];then echo "$file is regular file"elif [ -b $file ];then echo "$file is block file"elif [ -d $file ];then echo "$file is directory file"else echo "$file type unkonw"fi
3. for Loop
For I in {1 .. 10} # in is not necessarily followed by a number, as long as there is a command to return results
Do
Echo $ I
Done
Example 1: write a script to test IP addresses that can be used in the subnet
#! /Bin/bashfor I in {1 .. 50} do ping-c1 192.168.16. $ I &>/dev/null #-c1 indicates ping once if [$? -Ne 0]; then echo "192.168.16. $ I successful" echo "192.168.16. $ I" >>> ~ /Ipavailable.txt fidone ~
Example 2: count the number of each file type in/dev
#!/bin/bashdir='/dev'for i in $(ls $dir)do if [ -h $dir/$i ];then ((link+=1)) elif [ -f $dir/$i ];then (( rfile+=1)) elif [ -d $dir/$i ];then ((directory+=1)) elif [ -b $dir/$i ];then (( block+=1 )) else (( typeunknow+=1)) fidoneecho 'block' $blockecho 'regular file' $rfileecho 'directory' $directoryecho 'link' $linkecho 'unknow' $typeunknow
4. nested loop
Example 1: Output a 9-9 multiplication table
#!/bin/bashfor ((i=1;i<=9;i++))do for ((j=1;j<=i;j++)) do echo -n "$i*$j=$[$i*$j]" done echodone
Example 2: Verify the logon password of the user. After Successful Logon, run the command to exit after quit is entered.
#!/bin/bashuser='zhangcan'password='123'tag=truewhile $tagdo read -p 'input your name : ' name read -p 'input your password : ' code if [[ $name = $user ]] && [[ $code = $password ]];then echo 'login successful' while $tag do read -p '>>: ' cmd if [[ $cmd = 'quit' ]];then tag=false else $cmd fi done fidone