if, if/else, if /elif/else,case

A, if statement usage

if expression

then

command

be

 

Examples: integer comparison operators

read -p "please input a integer:" a

if [ "$a" -gt  10 ]

then

echo "The integer which you input is bigger than 10。"

exit 100

be

NOTE: After executing the script, exit command returns an exit status, echo $? You can print the exit status

 

Two, if / else statement Usage

if expression

then

command

else 

command

be

 

Example: to determine whether a file exists.

IF [-e " $ 1 "] # $ 1 is the location parameter

then

echo "the file $1 is exist"

else

echo "the file is $1 not exist"

be

 

 

Three, if / elif / else statement Usage

if expression1

then

command

elif expression2

then

command

elif expression3

then

command

else

command

be

 

Examples: input points, level determination score

#!/bin/bash
read -p "please input your score:" s
if [ "$s" -ge 90 ]
then
echo "The grade is A!"
exit 1
elif [ "$s" -ge 80 ]
then
echo "The grade is B!"
exit 2
elif [ "$s" -ge 70 ]
then
echo "The grade is C!"
exit 3
elif [ "$s" -ge 60 ]
then
echo "The grade is D!"
exit 4
else
echo "The grade is E!"
exit 5
fi

// running instance

[root@localhost lzic]# ./a.sh
please input your score:90
The grade is A!
[root@localhost lzic]# echo $?
1
[root@localhost lzic]# ./a.sh
please input your score:20
The grade is E!
[root@localhost lzic]# echo $?
5

Three, case usage

case variable in 

value1)

command

value2)

command

value3)

command

*)

command

esac

 

Guess you like

Origin www.cnblogs.com/97lzc/p/11222010.html