Chapter II Shell Script (ii)
the common options for test file testing are
-D: test for Catalog (directory)
-E: test whether the directory or file exists (exist)
-F: test for file
-R: Tests whether the current user has permission to read (read)
-W: Tests whether the current user has Write permission (write)
-x: Test whether the executable permission is set (excute)
There are two common expressions of Test
Test expression [ expression ]
The commonly used expression is the latter because it is more close to the programming habit, as follows
[Email protected] ~]# [-d/media/server/]; echo $? # Mates $ ? View, return value is not 0 Description no this directory
1
[[email protected] ~]# ld/media/server/# Verification
LD:/media/server/: No such file:no suchfile or directory
An integer value comparison contains options
-eq: equals (equal)
-ne: Not equal to (notequal)
-lt: Less than (lesser than)
-GT: Greater than (greater than)
-le: less than or equal (lesser or equal)
-ge: Greater than or equal to (greater or equal)
Integer value comparisons are applied more in scripts, such as judging the number of logged-in users.
[Email protected] shelltest]# stati= ' who | Wc-l '
[[Email protected] shelltest]# [$stati-le 5]&& echo "Active users less than 5 peopl"
Active users less than 5 peopl
string comparison
=: The first string is the same as the 2 string
! =: The first 1 strings are not the same as the 2 strings, "! "To take the opposite meaning
-Z: Checks if the character is empty.
Logic test
&&: Logical And, " and " means that when two conditions are true, the value is 0, and the test "&&" is used when testing can be changed to "-a"
|| : Logical OR, means " or ", as long as there is a condition, the entire test command return value is 0,test can be changed to "-O"
! : Logical No, meaning " no ", the return value of the entire test command is 0 only if the specified condition is not true .
use && to do an example:
[[Email protected] ~]# [6-ne 4] &&echo "Yes"
Yes
If Statement
the choice structure of the IF statement is divided into three basic types
If single branch structure, the format is as follows
If condition test action
Then
Command Sequence
Fi
650) this.width=650; "Width=" 553 "height=" 195 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>
Single-branch case
[Email protected] ~]# vim test.sh
#!/bin/bash
######## #if Single branch case ############
Who= ' Who |wc-l '
if [$who-le 5]; Then
echo "Active users less than 5people"
Fi
[Email protected] ~]# sh test.sh
Active users greater than 5 people
Dual Branch If Statement
Dual-Branch if statements require two different operations for " conditional " and " condition not established " two cases
650) this.width=650; "Width=" 554 "height=" [src=]/e/u261/themes/default/images/spacer.gif style= background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>
[Email protected] ~]# vim test.sh
#!/bin/bash
######## #if Multi-branch case ############
Who= ' Who |wc-l '
if [$who-le 5]; Then
echo "Active users less than 5people"
Else
echo "Active users greater than people"
Fi
[Email protected]~]# sh test.sh
Activeusers greater than 5 people
Multi-Branch if statement
Multi-Branch if statements perform different actions based on the test results, so they can be nested and used for multiple judgments. The format is as follows:
If single branch structure, the format is as follows
If condition test action 1
Then
command Sequence 1
Elif Condition test Action 2
Then
Command Sequence 2
Else
Command Sequence 3
Fi
650) this.width=650; "width=" 432 "height=" 189 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>
Multi-branch Case:
[Email protected] ~]# vim scores_query.sh
#!/bin/bash
########### score Excellent, good, poor query script ###########
Read-p " Please enter your score (1-100):" Score
If [$score-ge] && [$score-le100]
Then
echo " your score is $score points , for good "
elif [$score-ge] && [$score-le84]
Then
echo " your score is $score, good "
elif [$score-ge] && [$score-le 69]
Then
echo " your score is $score and pass "
elif [$score-le 59]
Then
echo " your score is $score, not in the grid "
Else
echo " Please enter a score between 1-100"
Fi
[Email protected] ~]# sh scores_query.sh
Please enter your score (1-100): 100
your score is a good
[Email protected] ~]# sh scores_query.sh
Please enter your score (1-100): 80
your score is
This article from "Addiction" blog, reproduced please contact the author!
Linux_shell Script Note II