Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

If conditional statement of Shell script

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Conditional test

Test command: tests whether a specific expression is valid. When the condition is true, the return value of the test statement is 0, otherwise it is another value.

Format 1: test conditional expression

Format 2: [conditional expression] (note that there is a space between the parentheses and the expression)

File testing:

Format: [operator file or directory] (note the space between the parentheses and the expression)

Commonly used test operator:-d: test whether it is a directory (Directory)-e: test whether a directory or file exists (Exist)-f: test whether it is a file (File)-r: test whether the current user has permission to read (Read)-w: test whether the current user has permission to write (Write)-x: test whether the current user has permission to execute (eXcute) [root@localhost ~] # [- D / media/cdrom] [root@localhost ~] # echo $? 0 / / returns 0 means the condition holds [root@localhost ~] # [- d / media/cdrom/Server] [root@localhost ~] # echo $? 1 / / return 1 means that the condition is not valid [root@localhost ~] # [- d / media/cdrom] & & echo "YES" / / "& &" means that when the previous test is true Then execute the following command YES [root@localhost ~] # [- r / media/cdrom] [root@localhost ~] # echo $? 1 [root@localhost ~] # [- w / media/cdrom] [root@localhost ~] # echo $? 1 [root@localhost ~] # [- x / media/cdrom] [root@localhost ~] # echo $? 1 [root@localhost ~] # integer value comparison:

Format: [integer 1 operator integer 2] (note the space between the parentheses and the expression)

Commonly used test operators:-eq: equal to (Equal)-ne: not equal to (Not Equal)-gt: greater than (Greater Than)-lt: less than (Lesser Than)-le: less than or equal to (Lesser or Equal)-ge: greater than or equal to (Greater or Equal) [root@localhost ~] # who | wc-17 [root@localhost ~] # [$(who | wc-l)-gt 5] & & echo "Too many." Too many. [root@. Localhost ~] # [$(who I wc-l)-ge10] & & echo "> = 10." Compare a string with a logical test string:

Format 1: [string 1 = string 2] or [string 1! = string 2] ("!" means inverted)

Format 2: [- z string]

The commonly used test operator: =: string content is the same! =: string content is different! Sign means the opposite-z: the string content is empty [root@localhost ~] # echo $LANGzh_ CN.UTF-8 [root@localhost ~] # [$LANG! = "en.US"] & & echo "Not en.US" / / testing whether the current locale Not en.US [root@localhost ~] # read-p "overwrites the existing file (yes/no)?" ACK / / Test Whether the string read is yes or overwrite the existing file (yes/no)? Yes [root@localhost ~] # [$ACK = "yes" I & & echo "overrides the logic test:

Format 1: [expression 1] operator [expression 2]

Format 2: command 1 operator command 2

Commonly used test operators:-an or &: logic and, "and" means-o or |: logic or, or!: logic No [root@localhost ~] # [- d / etc] & & [- r / etc] & & echo "You can open it" [root@localhost ~] # [- d / etc] II [- d / home] & echo "ok" if statement if single branch statement

Basic format:

If conditional Test Action then Command sequence fi

Example: determine the mount point directory and create it automatically if it does not exist.

[root@localhost ~] # vim chkmountdir.shangxunxinxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx / / execute if [!-d $MOUNT_DIR] then mkdir-p $MOUNT_ DIRfiif double branch statement when directory does not exist.

Basic format:

If conditional Test Action then Command sequence 1 else Command sequence 2fi

Example: determine whether the target host is alive or not, and display the test results.

The number of / dev/null / /-c packets, the number of seconds between-I, if [$?-eq 0] / / W waiting time if [$?-eq 0] / / to determine the test result of the ping command then echo "Host $1 is up." Else echo "Host $1 is down." fiif multi-branch statement

Basic format:

If conditional Test Action 1 then Command sequence 1elif conditional Test Operation 2 then Command sequence 2else Command sequence 3fi

Example: judge the range of scores and divide them into three grades: excellent, qualified and unqualified.

[root@localhost ~] # vim gradediv.shangxinghuanxinashanxinbashread-p "Please enter your score (0100):" GRADEif [$GRADE-ge 85] & & [$GRADE-le 100] then echo "$GRADE score, excellent!" elif [$GRADE-ge 70] & & [$GRADE-le 84] then echo "$GRADE score, qualified!" else echo "$GRADE score, not qualified!" fiif nested statement

In fact, the nested structure is to add a layer of if judgment statement to the previous three structures. Let me demonstrate it directly with an example. The main function of this Shell script is that when you enter a score, you will make a judgment. When the input score is less than 10 seconds (including 10 seconds), you will enter the finals, otherwise you will be eliminated. After entering the finals, they will be prompted to enter the gender to judge the grouping, and if the gender is male, they will be assigned to the male group, otherwise they will be assigned to the female group.

[root@localhost opt] # vim group.shroupplash binbinhread-p "Please enter your score:" scoif [$sco-le 10] then echo "Congratulations on reaching the finals!" Read-p "Please enter your gender:" sex if [$sex = "male"] then echo "you have been assigned to the male group!" Else echo "you have been assigned to the women's group!" Fielse echo "I'm sorry you've been eliminated!" Fi

The implementation results are as follows:

[root@localhost opt] # chmod + x group.sh [root@localhost opt] #. / group.sh Please enter your score: 9 Congratulations on reaching the finals! Please enter your gender: male, you have been assigned to the male group! [root@localhost opt] # / group.sh Please enter your score: 9 Congratulations on reaching the finals! Please enter your gender: female you have been assigned to the women's group! [root@localhost opt] # / group.sh Please enter your score: 11 I'm sorry you've been eliminated! [root@localhost opt] #

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report