SoFunction
Updated on 2025-03-09

Quick Start Shell Script Conditional Judgment Statements and Loops

I believe everyone has a certain understanding of shell scripts, and everyone must be very excited about the conditional judgment statements of shell scripts. In this blog, let’s talk about the conditional judgment statements and loops about shells.

1. Conditional judgment

1.1 Basic syntax

[condition]

Notice:

  • There must be spaces before and after the condition
  • The condition is not empty and true, [atguigu] sends back true, [] returns false

1.2. Commonly used judgment conditions

(1) Comparison between two integers = string comparison

  • -lt less than (less than) -le less equal)
  • -eq equals (equal) -gt greater than (greater than)
  • -ge greater equality -ne not equality

(2) Make judgments based on file permissions

  • -r has read permission (read)
  • -w has write permission (write)
  • -x has execution permission (execute)

(3) Make judgments according to file type

  • -f file exists and is a regular file (file)
  • -e file exists (existence)
  • -d file exists and is a directory (directory)

1.3. Case example

Is 23 greater than or equal to 22 Use $? View the result to true and return 0

[root@node01 shell]# [ 23 -ge 22 ]
[root@node01 shell]# echo $?
0

View the specific permissions for the file. Use $?View the result to true and return 0.

[root@node01 shell]# [ -w  ]
[root@node01 shell]# echo $?
0

Check whether the file in a directory exists? Use $? The result of viewing is true and returns 0.

[root@node01 shell]# [ -e /opt/shell/ ] --Not exists[root@node01 shell]# echo $?
1
[root@node01 shell]# [ -e /opt/shell/ ]
[root@node01 shell]# echo $?
0

Multi-condition judgment (&& means that the previous command is executed successfully, and the next command is executed only; || means that the next command is executed only after the previous command has failed)

[root@node01 shell]# [ condition ] && echo ok || echo notOK
ok
[root@node01 shell]# [ condition ] && [ ] ||echo notOk
notOk

1.Judgement

1.4.1 Basic syntax

if [Conditional judgment formula];then
program
fi
or
if [ Conditional judgment formula ]
 then
 program
fi #Ending Character

Notice:

  • [Conditional judgment formula], there must be spaces between brackets and conditional judgment formula
  • If there must be space after

1.4.2 Case practice

Enter a number, if it is 1, then the output is I am Zhang San, if it is 2, then the output is Li Si, if it is why not output.

[root@node01 shell]# vim 
if [ $1 -eq '1' ]
 then 
  echo 'I am Zhang San'
elif [ $1 -eq '2' ]
 then 
  echo 'I am Li Si' 
fi
[root@node01 shell]# sh  2
I'm Li Si
[root@node01 shell]# sh  1
I'm Zhang San
[root@node01 shell]# sh  3
[root@node01 shell]#

1.5 case statement

1.5.1 Basic syntax

case $Variable name in
 "Value 1")
   program1 ;;
 "Value 2")
   program2 ;;
 "Value n")
   programn ;;
 esac 

Notes:

  • The end of the case line must be the word "in", and each match must end with the closing bracket ")".
  • Double-divided ";;" means that the command sequence ends, which is equivalent to the break in Java search.
  • The last "*)" indicates the default mode, relative to the default in java.

1.5.2 Case practice

Enter a number. If it is 1, the squad leader will be output. If it is 2, the squad leader will be output. If it is something else, the shemale will be output.

[root@node01 shell]# vim  
#!/bin/bash
case $1 in
 '1')
  echo 'Squad Leader' ;;
 '2')
  echo 'Deputy Team Leader' ;;
 '*') 
  echo 'Shemale' ;;
esac 
[root@node01 shell]# sh  1
Squad Leader
[root@node01 shell]# sh  2
副Squad Leader
[root@node01 shell]# sh  3
Shemale

2. Loop

2.1 for loop

2.1.1 Basic Syntax 1

for ((Initial value;Cycle control conditions;Variable changes))
 do 
 program
done 

2.1.2 Case practice

Add from 1 to 100

**[root@node01 shell]# vim 
#!/bin/bash
num=0
for((i=0;i<=100;i++))
 do
 num=$[$num+$i]
done
echo $num
[root@node01 shell]# sh  
5050

2.1.3 Basic Syntax 2

for variable in value1 value2 value3 ..
 do
  program
done 

2.1.4 Example operation

Print all input parameters

hie[root@node01 shell]# vim  
#!/bin/bash
for i in $*
 do 
 echo " I love $i "
done
[root@node01 shell]# sh Zhang San Li Si I love Zhang San 
 I love Li Si 

Compare the difference between $* and $@

Both $* and $@ represent all parameters passed to a function or script, and cannot be included in double quotes "" and all parameters are output in the form of $1.$2…$n.

[root@node01 shell]# vim  
#!/bin/basn
for i in $*
do 
 echo " I love $i"
done

for j in $@
do 
 echo " I love $j"
done
[root@node01 shell]# 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 included in double quotes "", "$*" will output all parameters as a whole in the form of $1, $2, 3...3...n; "$@ will separate the parameters and output all parameters in the form of "$1", "$2", "3". . . . . . . . . . "3".... "3".... "n".

[root@node01 shell]# vim  
#!/bin/bash
for i in "$*"
do 
 echo " I love $i "
done
for j in "$@"
do
 echo " I love $j "
done
[root@node01 shell]# 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 loop

3.2.1 Basic syntax

while [Submit judgment]
do
 program
done 

3.2.2 Case practice

Add from 1 to 100

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

This is the article about the conditional judgment statements and loops of a quick start shell script. For more relevant shell conditional judgment statements and loop content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!