variable test Statement-test
Function: Used to test whether the variable is equal, is empty, file type, and so on.
Format:
Test tested Condition or []#范围: Integer, String. File
1) integer test :
Test int1-eq int2 test integers are equal
Test int1-ge int2 Test int1 whether >=int2
Test int1-gt int2 Test int1 whether >int2
Test int1-le int2 test int1 whether <=int2
Test int1-lt int2 Test int1 whether <int2
Test int1-ne int2 test integers are not equal
2) string test :
Test STR1=STR2 test strings are equal
Test STR1!=STR2 test string is not equal
Test str1 test string is not empty
Test-n str1 test string is not empty
Test-z str1 test string is empty
3) file Test :
test-d file Specifies whether files are folders
Test- F file Specifies whether files are regular files
Test-x file Specifies whether files can be run
Test-r file Specifies whether the files are readable
Test-w file Specifies whether files are writable
test-a file Specifies whether the file exists
test-s file the size of the file is not 0
Note: Test test statements are generally not used alone , generally as a test condition of the IF statement , such as ;
If test-d filethen....fi
The abbreviated form of the variable for test "[]"
Demo Sample -apachtest.sh
#!/bin/bash# A Test shell script for test Apache was running or notweb=$ (/usr/bin/pgrep httpd) echo "Now let's test the APAC He ... "echo#if [" $web "! =" "]if [-N" $web "]then echo" Apache is running ... "Else echo" Apache is not running ... " /etc/rc.d/init.d/httpd Startfi
Process Control Statements
Flow control Statement : The process used to control the Shell program
Exit statement : exit the program to run , and return a return code , return code 0 indicates normal exit , non- 0 indicates an abnormal exit.
For example : Exit 0
First, if
if/ Then format
Demo Sample -if_then.sh
#!/bin/bash# A Test shell script for if/thenif [-x/etc/rc.d/init.d/httpd]then echo "script:/ETC/RC.D/INIT.D/HTTDP Have x power! " /ETC/RC.D/INIT.D/HTTPD Restartfi
If/else Format
Union of multiple conditions :
-A: Logical and , the result is true only if two conditions are set .
-O: Logical OR , Two conditions only need to have a set up , The result is true.
Demo Sample-if_else.sh
#!/bin/bash# a test shell script for if/elif/elseecho-n "please input A filename:" Read Filenameif [-D $filename]then
echo "$filename is a directory" elif [-F $filename]then echo "$filename is a commen file" elif [-C $filename-O-B $filename]then echo "$filename is a device file" Else echo "$filename is a unkown file" fi
Demo sample -if_elif_exit.sh
#!/bin/bash# A Test shell script for If/elifif [$#-ne 2] thenecho ' Not enough parameters ' exit 1fiif [$1-gt $]then
echo "is great then $" elif [$1-lt $]then echo "is little then $" Else echo "is equal as $" fi
Second,for/in
Demo Sample-for.sh
#!/bin/bash# A Test shell script for ' for ' for day in Sunday Monday Tuesday Wednesday Thursday Friday Saturdaydo echo " The day was $DAY "done
awk Command [Segment Extraction]
Awk-f field delimiter ' command '[single citation] #假设不用- f to specify the cut character, the default space
1 . The user with UID 0 in thedetection system
Awk-f: ' $3==0 {print '} '/etc/passwd
#awk-F: ' {print $} '/etc/passwd
-F: Specifies that the cut is attached as:
$ A: third place for cutting
2, the detection system in the password is empty users
Awk-f: ' Length ($) ==0 {print $} '/etc/shadow
#ps aux | Grep-v Root | awk ' {print $} '
Demo Sample -awk.sh
#!/bin/bash# a test script for desplay users infomation/bin/echo-n "Please input A username:" Read Username/bin/grep $use rname/etc/passwd >/dev/null 2>/dev/nullif [$?-eq 0]then/bin/echo "username is: $username" Else/bin/echo "User: $username is not exits." Exit 1fi/bin/echo# list/etc/passwd infouserinfo= '/bin/grep ^ $username: x/etc/passwd ' uid= ' echo $userinfo | Awk-f: ' {print $} ' gid= ' echo $userinfo | Awk-f: ' {print $4 '} ' dir= ' echo $userinfo | Awk-f: ' {print $6} ' shell= ' echo $userinfo | Awk-f: ' {print $7} ' # Get/etc/group infogroupinfo= '/bin/grep x: $gid/etc/group ' gname= '/bin/echo $groupinfo | Awk-f: ' {print $} '/bin/echo ' User ID is: $uid "/bin/echo" default group is: $gname "/bin/echo" Home directory is: $dir "/b In/echo "Shell is: $shell"/bin/echo "group member Info:" # Get Group membersgroups= '/usr/bin/groups $username '/bin/echo $g roups/bin/echo# get online infoonline= '/usr/bin/who | grep $username ' If [-Z ' $online "]then echo" $username is not online "ElSe echo "$username is online ..." fi
Example -killuser.sh
#思路: Shutting down a user's entire process containment shell is equivalent to kicking the user out of the system #!/bin/bash# a shell sript to kill a user in linuxusername=$1killpid= '/bin/ps aux | grep $username | awk ' {print $} ' for PID in $killpiddo /bin/kill-9 $PID 2>/dev/nulldone
Getting Started with Shell Programming (second Edition) (middle)