Linux shell programming condition judgment-if, while, for, case

Linux shell programming condition judgment-if, while, for, case

if syntax format

if condition 
then statements 
[elif condition 
    then statements] 
[else 
    statements] 
fi

Example

vi if.sh

#!/bin/bash
if [$1 = root]
then
   echo "hello $2, welcome !"
elif [$1 = intsmaze]
then
   echo "hello ${2}, welcome !"
else
   echo "SB${2}, get out here !"
fi

sh if.sh intsmaze nihao When executing the script, pass in the parameters after the script command.

Analyzing conditions

1: Basic syntax of conditional judgment

[condition]  (note that there must be spaces before and after the condition)

#Non-empty returns true, can use $? verification (0 is true, 1 is false)

#Empty returns false, the condition in which is a space is also called empty.

[hadoop@centos-Reall-131 shtest]$ if [1 ]; then echo ok ;else echo bad; fi; 
ok
[hadoop@centos-Reall-131 shtest]$ if [0 ]; then echo ok ;else echo bad; fi;  
ok
[hadoop@centos-Reall-131 shtest]$ if [a ]; then echo ok ;else echo bad; fi;  
ok
[hadoop@centos-Reall-131 shtest]$ if []; then echo ok ;else echo bad; fi;    
bad
[hadoop@centos-Reall-131 shtest]$ if []; then echo ok ;else echo bad; fi; 
bad

Note that the inside of []= the surrounding spaces, there is a difference:

[hadoop@centos-Reall-131 shtest]$ if [a=b ]; then echo ok ;else echo bad; fi;  
ok
[hadoop@centos-Reall-131 shtest]$ if [a = b ]; then echo ok ;else echo bad; fi; 
bad

#a=b It is recognized as a whole string, so the condition is judged to be true

Short circuit (understood as a ternary operator)

[condition] && echo OK || echo notok

If the condition is met, execute the statement following &&; if the condition is not met, execute the statement following ||

2: Conditional judgment combination

Note: The difference between [] and [[ ]]: the logical combination in [[ ]] can use && (and), || (or), and the logical combination in [] can use -a (and), -o (or ).

[hadoop@centos-Reall-131 shtest]$ if [a = b && b = c ]; then echo ok;else echo bad;fi  
-bash: [: missing `]'
bad
[hadoop@centos-Reall-131 shtest]$ if [a = b -ab = b ]; then echo ok;else echo bad;fi  
bad
[hadoop@centos-Reall-131 shtest]$ if [a = b -ob = b ]; then echo ok;else echo bad;fi  
ok
[hadoop@centos-Reall-131 shtest]$ if [[ a = b && b = b ]]; then echo ok;else echo bad;fi  
bad
[hadoop@centos-Reall-131 shtest]$ if [[ a = b || b = b ]]; then echo ok;else echo bad;fi  
ok

3: Commonly used judgment operators

String comparison : =, !=, -z returns true if the string length is 0, -n returns true if the string length is not 0, and spaces are not included here.

[hadoop@centos-Reall-131 shtest]$ if ['aa' ='bb' ]; then echo ok; else echo bad;fi #quotation marks are optional, of course, it is better to use quotation marks when comparing .
bad
[hadoop@centos-Reall-131 shtest]$ if [-n "aa" ]; then echo ok; else echo bad;fi
ok
[hadoop@centos-Reall-131 shtest]$ if [-z "" ]; then echo ok; else echo bad;fi
ok

Integer comparison:

-lt is less than

-le is less than or equal to

-eq is equal to

-gt is greater than

-ge is greater than or equal to

-ne is not equal to

File judgment:

-d is a directory

-f is a file

-e exists

[hadoop@centos-Reall-131 shtest]$ if [-d/home/hadoop ]; then echo ok; else echo bad;fi    
ok
[hadoop@centos-Reall-131 shtest]$ if [-f/home/hadoop/]; then echo ok; else echo bad;fi      
bad
[hadoop@centos-Reall-131 shtest]$ if [-e/home/hadoop/]; then echo ok; else echo bad;fi      
ok

The path must be found. If the file with the specified path is not found, the judgment condition is false.

while syntax

i=1
while ((i<=3))
do
  echo $i
  let i++ # (equivalent ((i++)))
done

case syntax

case $1 in
start)
echo "starting"
;;
stop)
echo "stoping"
;;
*)#If other conditions do not match, execute this
echo "Usage: {start|stop}"
esac

for syntax

1. Method one
N=1
for N in 1 2 3
do
echo $N
done
or
N=1
for N in 1 2 3; do echo $N; done
or
N=1
for N in {1..3}; do echo $N; done # (represents a number between 1 and 3)
2. Method two
for ((i = 0; i <= 5; i++))
do
echo "welcome $i times"
done

or

for ((i = 0; i <= 5; i++)); do echo "welcome $i times";done