Quick start conditional statement and loop of shell script

Time:2021-9-17

I believe you have a certain understanding of shell script, and you must be looking forward to the conditional judgment statements of shell script. In this blog, let’s talk about conditional judgment statements and loops of shell.

1. Condition judgment

1.1 basic grammar

[condition]

be careful:

  • There should be a space before and after condition
  • If the condition is not empty, it is true, [atguigu] sends back true and [] returns false

1.2. Common judgment conditions

(1) Comparison between two integers = string comparison

  • -LT less than – Le less equal
  • -EQ equals (equal) – GT is greater than (greater than)
  • -Ge greater than or equal – Ne not equal

(2) Judge according to file permissions

  • -R has read permission (read)
  • -W has write permission (write)
  • -X has execute permission (execute)

(3) Judge by file type

  • -The f file exists and is a regular file
  • -E file exists
  • -D file exists and is a directory

1.3. Case examples

Is $used when 23 is greater than or equal to 22? The viewing result is true and returns 0


[[email protected] shell]# [ 23 -ge 22 ]
[[email protected] shell]# echo $?
0

View file specific permissions using $? The viewing result is true and returns 0


[[email protected] shell]# [ -w helloword.sh ]
[[email protected] shell]# echo $?
0

Check whether the files in a directory exist. Use $? The viewing result is true and returns 0

[ [email protected] Shell]# [- E / opt / shell / AA. TXT] - does not exist
[[email protected] shell]# echo $?
1
[[email protected] shell]# [ -e /opt/shell/helloword.sh ]
[[email protected] shell]# echo $?
0

Multi condition judgment (& & indicates that the previous command is executed successfully, and | indicates that the next command is executed after the previous command fails)


[[email protected] shell]# [ condition ] && echo ok || echo notOK
ok
[[email protected] shell]# [ condition ] && [ ] ||echo notOk
notOk

1.4. If judgment

1.4.1 basic grammar

If [conditional judgment formula]; then
program
fi
perhaps
If [conditional judgment]
 then
 program
Fi # Terminator

be careful:

  • [conditional judgment], there must be a space between brackets and conditional judgment
  • If should be followed by a space

1.4.2 case practice

Enter a number. If it is 1, I will be output as Zhang San. If it is 2, I will be output as Li Si. If it is anything, I will not be output.

[[email protected] shell]# vim if.sh
if [ $1 -eq '1' ]
 then 
  Echo 'I'm Zhang San'
elif [ $1 -eq '2' ]
 then 
  Echo 'I'm Li Si' 
fi
[[email protected] shell]# sh if.sh 2
I'm Li Si
[[email protected] shell]# sh if.sh 1
I'm Zhang San
[[email protected] shell]# sh if.sh 3
[[email protected] shell]#

1.5 case statement

1.5.1 basic grammar

Case $variable name in
 "Value 1")
   Procedure 1;;
 "Value 2")
   Procedure 2;;
 "Value n")
   Program n;;
 esac

matters needing attention:

  • The end of the case line must be the word “in”, and each match must end with a closing parenthesis “)”.
  • Double score “;” indicates the end of the command sequence, which is equivalent to the break in Java.
  • The last “*” indicates the default mode, which is relative to the default in Java.

1.5.2 case practice

Enter a number. If it is 1, the monitor will be output; if it is 2, the Deputy monitor will be output; if it is other, the human demon will be output.

[[email protected] shell]# vim case.sh 
#!/bin/bash
case $1 in
 '1')
  Echo 'squad leader';;
 '2')
  Echo 'Deputy monitor';;
 '*') 
  Echo 'human demon';;
esac 
[[email protected] shell]# sh case.sh 1
monitor
[[email protected] shell]# sh case.sh 2
副monitor
[[email protected] shell]# sh case.sh 3
Simon?

2. Circulation

2.1 for loop

2.1.1 basic grammar 1

For ((initial value; loop control condition; variable change))
 do 
 program
done

2.1.2 case practice

From 1 to 100


**[[email protected] shell]# vim for1.sh
#!/bin/bash
num=0
for((i=0;i<=100;i++))
 do
 num=$[$num+$i]
done
echo $num
[[email protected] shell]# sh for1.sh 
5050

2.1.3 basic grammar 2

For variable in value 1 value 2 value 3
 do
  program
done

2.1.4 example practice

Print all input parameters

hie[[email protected] shell]# vim for2.sh 
#!/bin/bash
for i in $*
 do 
 echo " I love $i "
done
[ [email protected] Shell]# sh for2.sh Zhang San Li Si
 I love Zhang San 
 I love Li Si

Compare the differences between $* and [email protected]

Both $* and [email protected] represent all parameters passed to a function or script and cannot be contained in double quotation marks. Yes, all parameters are output in the form of $1. $2… $n.

[[email protected] shell]# vim for3.sh 
#!/bin/basn
for i in $*
do 
 echo " I love $i"
done

for j in [email protected]
do 
 echo " I love $j"
done
[ [email protected] Shell]# sh for3.sh Zhang San Li Si Wang Wu
 I love Zhang San
 I love Li Si
 I love Wang Wu
 I love Zhang San
 I love Li Si
 I love Wang Wu

When they are contained in double quotation marks, “$*” will output all parameters as a whole in the form of $1, $2,3… 3… 3… N; “[email protected] will separate each parameter and output all parameters in the form of” $1 “,” $2 “,” 3 “….” 3 “….” 3 “….” n “.

[[email protected] shell]# vim for4.sh 
#!/bin/bash
for i in "$*"
do 
 echo " I love $i "
done
for j in "[email protected]"
do
 echo " I love $j "
done
[ [email protected] Shell]# sh for4.sh Zhang San Li Si Wang Wu
 I love Zhang San Li Si Wang Wu 
 I love Zhang San 
 I love Li Si 
 I love Wang Wu

3.2 whlie cycle

3.2.1 basic grammar

While [submit judgment]
do
 program
done

3.2.2 case practice

From 1 to 100


[[email protected] shell]# vim while.sh 
#!/bin/bash
num=0
i=1
while [ $i -le 100 ]
do
 num=$[$num+$i]
 i=$[$i+1]
done
echo $num
[[email protected] shell]# sh while.sh 
5050

This is the end of this article about the conditional judgment statements and loops of quick start shell scripts. For more information about shell conditional judgment statements and loops, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]