Shell programming condition test if, for, case, select, while

Keywords: Linux shell vsftpd less

shell programming condition test

Shell Conditional Judgment Initial
Shell branch if statement
Shell Loop for Statement
Shell branch case statement
Select List Select Loop
Shell loop controls continue,break,exit
Shell loop while statement

Shell Conditional Judgment Initial

[] Represents a conditional test, expanded to a test command
 [] It is important to note the spaces here.Note that spaces are required after'['and before']'

For example:
STRING1 = STRING2 string equal
 STRING1!= STRING2 strings are unequal
[root@linux ~]# test "root" = "root"
[root@linux ~]# echo $?
0
[root@linux ~]# test "root" = "Root"
[root@linux ~]# echo $?
1
[root@linux ~]# [ "root" != "ROOT" ]
[root@linux ~]# echo $?
0


$RANDOM%10 Random Number Remainder 0-9
guess=$(($RANDOM%10+1))


Automatically fill in password 123 in a non-interactive manner (avoid interaction)
echo 123 | passwd --stdin user$i &> /dev/null



Integer judgment
 INTEGER1-eq INTEGER2 equal 
INTEGER1-ge INTEGER2 greater than or equal to
 INTEGER1-gt INTEGER2 greater than
 INTEGER1-le INTEGER2 is less than or equal to
 INTEGER1-lt INTEGER2 is less than
 INTEGER1-ne INTEGER2 is not equal to
 For example:
[root@linux ~]# [ 100 -gt 90 ]
[root@linux ~]# echo $?
0


File Type Judgment
 -d FILE file exists and is a directory
 -e FILE file exists
 -f FILE file exists and is a normal file (ls-l first is -)
-s FILE file exists and is not empty


File Permission Judgment
 -r FILE file exists and has read permissions
 -w FILE file exists and has write permissions
 -x FILE file exists and has execute (or search) permissions


Shell branch if statement
  • Single Branch Structure
if [ condition1 ] 
 then action1 
fi 
[root@linux ~]# cat test.sh
#!/bin/bash
if [ -f /etc/passwd ]
then
  echo "This is a file"
fi

  • Double Branch Structure
if [ condition1 ] 
 then action1   Act when conditions are true
else 
 //action2    Conditional Holidays Actions
fi 


[root@linux ~]# cat test.sh
#!/bin/bash
if [ -f /etc/vsftpd/vsftpd.conf ]
then
  service vsftpd start
else
  echo "vsftpd is not install"
fi


//Prompt for a user name,Determine if the user exists?Exists to display its information(uid gid Home catalog shell),Adding this account number does not exist
#!/bin/bash
read -p "enter one user name: " username
if id $username &> /dev/null
  then
    echo "User Exists,display information"
    grep $username /etc/passwd | cut -d':' -f 1,3,4,6,7
  else
    echo "user $username Non-existent,Add User"
    useradd $username
fi

  • Multi-Branch Structure
    If condition 1 is true, execute statement 1, if false, judge condition 2, if condition 2 is true, execute statement 2...If all conditions are false, execute statement n.
if [ condition1 ] 
  then action1 
elif [ condition2 ] 
  then action2 
... ... 
else 
  //Action n 
fi 

[root@linux ~]# cat test.sh
#!/bin/bash
guess=$(($RANDOM%100+1))
read -p "Please enter 1-100 integer: " NUM
if [ $NUM -eq $guess ]
then
  echo "congratulation"
elif [ $NUM -gt $guess ]
then
  echo "big!!! try again !!!"
else
  echo "small!!! try again !!!"
fi


Shell Loop for Statement

for variable in value1 value2 value3 ... ... 
for ((i=1;i<=5;i++))
do 
  //action1 
  //action2 
  ... ... 
done 

[root@linux ~]# for ((i=1;i<=5;i++)); do echo $i ; done


Shell branch case statement

case variable in 
Mode 1) 
  Action 1 
;; 
Mode 2) 
  Action 2 
;; 
... ... 
Mode N) 
  Action N 
;; 
*) 
  action 
;; 
esac 

[root@linux ~]# cat test.sh
#!/bin/bash
 Read-p "Please show me your identity:" name
case name in
captain)
  echo "Don't shoot Captain!"I am ~"
;;
hulk)
  echo "Did you drink Sanlu milk powder?"
;;
ironman)
  echo "Where did you buy your nails?""
*)
  echo"Reminder for entering information"
;;
esac


Selectect List Loop

select variable in command1 command2 command3 ... ... 
do 
  //action 
done

[root@linux ~]# cat est.sh
#!/bin/bash
select i in date cal pwd
do
  $i
done


[root@linux ~]# ./test.sh
1) date
2) cal
3) pwd
#? 1
Sun Nov 16 18k49k45 CST 2014


Shell loop controls continue,break,exit

break       Jump out of the whole cycle
continue    Jump out of the current loop and do not execute any morecontinueSubsequent statement,Go to the next loop.
exit        immediate withdrawal


[root@linux ~]# cat test.sh
#!/bin/bash
# continue
for i in {1..10}
do
  if [ $(($i%3)) -eq 0 ]
  then
# continue jumps out of this loop and goes back to the for loop above
# break jumps out of the whole loop
  else
    echo "$i"
done



Shell loop while statement
When the condition behind while is true, the statement between do and do is executed until the condition is false.

while condition 
do 
  //action1 
  //action2 
 ... ... 
done


//Dead cycle
[root@linux ~]# while true ; do echo 1 ; done

[root@linux ~]# cat tset.sh
#!/bin/bash
x=0
while [ $x -le 10 ]
do
  echo $x
done


11 original articles published, 0 praised, 88 visits
Private letter follow

Posted by guxin1999 on Wed, 12 Feb 2020 18:42:12 -0800