majun@instance-zqtg07w6:~$ vim score01.sh
majun@instance-zqtg07w6:~$ cat score01.sh
#!/bin/bashecho -n "Please input your score:"read SCORE
if["$SCORE" -lt 60 ];thenecho"C"fiif["$SCORE" -lt 80 -a "$SCORE" -ge 60 ];thenecho"B"fiif["$SCORE" -ge 80 ];thenecho"A"fi
majun@instance-zqtg07w6:~$ bash score01.sh
Please input your score:99
A
majun@instance-zqtg07w6:~$ bash score01.sh
Please input your score:78
B
majun@instance-zqtg07w6:~$ bash score01.sh
Please input your score:59
C
majun@instance-zqtg07w6:~$
if/else 判断结构
if expression;thencommandelsecommandfi
majun@instance-zqtg07w6:~$ vim check_file.sh
majun@instance-zqtg07w6:~$ cat check_file.sh
#!/bin/bashecho -n "input your file:"read FILE
#FILE=/etc/passwdif[ -e $FILE];thenecho"$FILE exists."elseecho"$FILE NOT EXISTS."fi
majun@instance-zqtg07w6:~$ bash check_file.sh
input your file:/etc/passwd
/etc/passwd exists.
majun@instance-zqtg07w6:~$ bash check_file.sh
input your file:/etc/passwd01
/etc/passwd01 NOT EXISTS.
majun@instance-zqtg07w6:~$
if/elif/else 判断结构
if expression;then
command1
elif expression;then
command2
elif expression;then
command3
else
command4
fi
majun@instance-zqtg07w6:~$ cat score03.sh
#!/bin/bashecho -n "Please input your score:"read SCORE
if["$SCORE" -lt 60 ];thenecho"CCCCCCCC"elif["$SCORE" -ge 60 -a "$SCORE" -lt 80 ];thenecho"BBBBBBBB"elseecho"AAAAAAAA"fi
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:99
AAAAAAAA
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:80
AAAAAAAA
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:59
CCCCCCCC
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:78
BBBBBBBB
majun@instance-zqtg07w6:~$
case 判断结构
majun@instance-zqtg07w6:~$ vim os_type.sh
majun@instance-zqtg07w6:~$ cat os_type.sh
#!/bin/bash
OS=`uname -s`case"$OS"in
Linux)echo"this is linux;";;
SunOS)echo"this is sun";;
*)echo"failed to type os";;
esac
echo$OS
majun@instance-zqtg07w6:~$ bash os_type.sh
this is linux;
Linux
majun@instance-zqtg07w6:~$
```