Proficient in shell programming for four days (3)
Four-condition test
Test test condition test content
[test condition test content] # is more widely used
1 Test file type
Test-e filename tests whether the file exists and is true
[- e file name] Note: spaces must be added on the left and right sides of []!
Attachment: test test results do not show, need to use echo$? To display the return value of test
The return 0 statement is correct, and a non-0 statement is incorrect.
Extension: test-e test.txt & & echo Yes | | echo No
Test-f file name determines whether it is a normal file # if it is a file, it returns 0, if the directory returns non-0
Test-d file name determines whether it is a directory or not
Test-b file name determines whether it is a block device file
Test-c filename character device file
2 Test file permissions
Test-r file name determines whether it has readable permissions
The test-w file name is writable
Test-x filename execution
Test-s file name determines whether it is non-blank and whether the content is true.
3 comparison of two documents
[file1-nt file2] whether file1 is newer than file2
[file1-ot file2] whether file1 is older than file2
[file1-ef file2] whether file1 and file2 are linked files, shortcuts, soft links
4. Judge between two values
[N1-eq N2] N1 and N2 are equal # it is recommended to use the following = = symbol to judge
Test: aa=123;bb=123
[$aa-eq $bb] & & echo Yes | | echo no
[N1-ne N2] N1 and N2 are not equal
[N1-gt N2] N1 is greater than N2
[N1-lt N2] N1 is less than N2
[N1-ge N2] N1 is greater than or equal to N2
[N1-le N2] N1 is less than or equal to N2
5 judgment string
[- z string] determines whether the string is empty, and the variable value is true if it has no content / is empty
[string 1 = = string 2] determines whether string 1 is equal to string 2 # determines whether string type is used
[$aa = = $bb] & & echo 1 | | echo 2
Attached: aa=abc
["$aa" = = "abc"] & & echo Yes | | echo No # Why is the test successful without two ""?
[string 1! = string 2] determine whether the string is unequal
6 multiple judgments
-a logic vs.
[- z $file-a-e $file]
-o logical or
! Logic is not
Example 7: determine the type of file entered and file permissions Note: be sure to practice
#! / bin/bash
Echo-e "nide wenjian shifou cunzai? wenjian quanxian shi shenme?\ n\ n"
Read-p "please input filename:"-t 30 filename
Test-z $filename & & echo "please input filename!" & & exit 1
# determine whether the string is empty, if so, print an error message, and exit the program!
Test!-e $filename & & echo "wenjian bucunzai!" & & exit 2
The #-e variable determines whether the file exists and whether it exists is true! Logic is not
Test-f $filename & & filetype=putong
Whether #-f is an ordinary file
Test-d $filename & & filetype=mulu
#-d whether it is a directory
Test-r $filename & & perm= "read"
#-r whether it has readable permission
Test-w $filename & & perm= "$perm write"
Whether #-w is writable
Test-x $filename & & perm= "$perm executable"
Whether #-x is executable
Echo-e "the filename is: $filename\ n"
# print file name
Echo-e "filetype is: $filetype\ n"
# print file type
Echo-e "permision is: $perm\ n"
# permission to print files
Five process control
1 if statement
1) if conditional statement-- single branch. Perform the appropriate action when the condition is established
Format:
If conditional test command
Then command sequence
Fi
Example 8:
If the space usage of the / boot partition exceeds 80%, output a warning
#! / bin/bash
RATE= `df-hT | grep "/ boot" | awk'{print $6}'| cut-d "%"-F1 `#: backquotes!
#: df command: displays partition usage
If [$RATE-gt 80]
Then
Echo "Warning,/boot DISK is full!"
Fi
2) if conditional statement-double branches. Perform different actions when "conditions are established" and "conditions are not valid"
Format:
If conditional test command
Then command sequence 1
Else Command sequence 2
Fi
Example 9:
Determine whether the httpd service is started, and start if it is not started
#! / bin/bash
Http= `netstat-tlun | awk'{print $4 "\ n"}'| grep ": 80 $" `
(or http=$ (ps aux | grep httpd | grep-v grep))
Attached: netstat-tlun is used to view all ports started by the system
Psaux is used to print all the processes of apache
Grep-v grep reverses lines that contain grep
If [- z "$http"]
Then
Echo "httpd meiyou qidong!"
/ etc/rc.d/init.d/httpdstart
Else
Echo "httpd runing"
Fi
Add: stop Apache:servicehttpd stop
3) if conditional statement-multi-branch
Format:
If conditional Test Command 1; then
Command sequence 1
Elif conditional Test Command 2; then
Command sequence 2
Elif...
Else
Command sequence n
Fi
Example 10:
# judge the character entered
#! / bin/bash
Echo "if you want to beijing, please input 1"
Echo "if you want to shanghai, please input 2"
Echo "if you want to chengdu, please input 3"
Read-p "please input a num:"-t 30 num
If ["$num" = = "1"]
Then
Echo "beijingling cake!"
Elif ["$num" = = "2"]
Then
Echo "shanghaixiajiao!"
Elif ["$num" = = "3"]
Then
Echo "Chengduqiqiao!"
Else
Echo "error,please input 1 or 2 or 3."
Fi