Basic judgment of Shell script
one。 Conditional test format 1: test conditional expression format 2: [conditional expression] II. File test-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] ~] # test-d / etc/yum / / Test whether / etc/yum is the directory [root@localhost ~] # echo $? / / the last command execution status 0 is normal execution Non-0 is error 0 [root@localhost ~] # test-f / etc/yum / / Test / etc/yum is the file [root@localhost ~] # echo $? 1 [root@localhost ~] # test-e / etc/yum / / Test / etc/yum exists [root@localhost ~] # echo $? 0 III. Integer test-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-l)-gt 10] & & echo "a little too much!" / Statistics the current number of users is a little too much! Four. string comparison format 1: [1 = = 2] format 2: [- z 2] =: the string content is the same! =: the string content is different! It means the opposite-z: the string content is empty five. Logic test-an or &: logic and, "and" means-o or | |: logic or, or! Logic No [root@localhost ~] # [!-e / opt/abc] & & mkdir / opt/abc / opt/abc this directory does not exist, I will create this directory [root@localhost ~] # ls / optabc rh6.if statement single branch! [] (https://s1.51cto.com/images/blog/201910/09/a9f39959fb7e9ee3c80634b282a7ac5d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10, Yicheng 10 mulu shadowbox 90 title typewriter ZmFuZ3poZW5naGVpdGk =) #! / bin/bashmulu= "/ etc/ccc" if [!-d $mulu] then mkdir-p $mulufi7.if double branch statement
#! / bin/bashping-c 3-I 0.2-W 3 $1 & > / dev/nullif [$?-eq 0] then echo "Host $1 is up" else echo "Host $1 is down" fi#-C send data packets,-0.2 send every two seconds-W 3 times reply all output to this useless directory [root@localhost ~] # source test.sh Host is down 8.if multiple branches and nested statements
#! / bin/bash# A running race, reaching the finals in 10 seconds After entering the finals, you will be divided into the men's group or the women's group read-p "Please enter your match time" timif [$stim-lt 10] then echo "enter the finals" read-p "enter your gender (male / female)" sex if [$sex = "male"] then echo "enter the male group" else echo "enter the female group" fielse "be eliminated" fi