一条件选择语句——if语句
(一)if条件语句的语法
2双分支结构
3多分支结构
条件为真的分支代码
elif 判断条件 2 ; then
条件为真的分支代码
elif 判断条件 3 ; then
条件为真的分支代码
else
以上条件都为假的分支代码
fi
逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句
多分支的if语句和默认路由是一样的,网络都不匹配的情况下走默认路由。
[root@centos7 ~]# type if
if is a shell keyword
[root@centos7 ~]# help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi 中括号表示可有可无的 Execute commands based on conditional. The `if COMMANDS' list is executed. If its exit status is zero, then the `then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list is executed in turn, and if its exit status is zero, the corresponding `then COMMANDS' list is executed and the if command completes. Otherwise, the `else COMMANDS' list is executed, if present. The exit status of the entire construct is the exit status of the last command executed, or zero if no condition tested true. Exit Status: Returns the status of the last command executed.
示例
1、编写脚本createuser.sh
实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
完整脚本
[root@centos73 shell_scripts]# cat createuser.sh
#!/bin/bash
#Author=wang
if [ $# -ne 1 ] ; then echo "you must enter a parameter" exit 1 fi #先判断是否有参数,有和没有是一回事,这个容易被忽视 username=$1 #变量就是第1个参数 id $username &>/dev/null if [ $? -eq 0 ] ; then echo "$username has existed !" else useradd $username &>/dev/null fi id $username
执行结果
[root@centos73 shell_scripts]# bash createuser.sh wu
wu has existed !
uid=1023(wu) gid=1023(wu) groups=1023(wu) [root@centos73 shell_scripts]# bash createuser.sh wang wang has existed ! uid=1022(wang) gid=1022(wang) groups=1022(wang) [root@centos73 shell_scripts]# bash createuser.sh xixixi uid=1025(xixixi) gid=1025(xixixi) groups=1025(xixixi)
升级版,显示颜色
[root@centos73 shell_scripts]# cat createuser_1.sh
#!/bin/bash
#Author=wang
RED="\033[31m" YEW="\033[0;33m" RESET="\033[0m" if [ $# -ne 1 ] ; then echo -e "$RED you must enter a parameter$RESET" #-e启用反斜杠转义的解释 exit 1 fi username=$1 id $username &>/dev/null if [ $? -eq 0 ] ; then echo -e "$YEW$username has existed !$RESET" else useradd $username &>/dev/null fi id $username
2、编写脚本yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
完整脚本
[root@centos73 shell_scripts]# cat yesorno.sh
#!/bin/bash
#Author=wang
read -p "do you agree (yes/no):" choice yes="^[Yy]([Ee][Ss])?$" no="^[Nn]([Nn])?$" #行首行尾锚定 if [[ "$choice" =~ $yes ]] ; then echo "you enter yes" elif [[ "$choice" =~ $no ]] ; then echo "you enter no " else echo "you enter not a yes or no" fi
执行结果
[root@centos73 shell_scripts]# bash yesorno.sh
do you agree (yes/no):yes
you enter yes
[root@centos73 shell_scripts]# bash yesorno.sh
do you agree (yes/no):YES you enter yes [root@centos73 shell_scripts]# bash yesorno.sh do you agree (yes/no):Yes you enter yes [root@centos73 shell_scripts]# bash yesorno.sh do you agree (yes/no):YEs you enter yes
[root@centos73 shell_scripts]# bash yesorno.sh
do you agree (yes/no):NO
you enter no
[root@centos73 shell_scripts]# bash yesorno.sh
do you agree (yes/no):no you enter no [root@centos73 shell_scripts]# bash yesorno.sh do you agree (yes/no):No you enter no [root@centos73 shell_scripts]# bash yesorno.sh do you agree (yes/no):nO you enter no
[root@centos73 shell_scripts]# bash yesorno.sh
do you agree (yes/no):df
you enter not a yes or no
[root@centos73 shell_scripts]# bash yesorno.sh
do you agree (yes/no):1234f you enter not a yes or no [root@centos73 shell_scripts]# bash yesorno.sh do you agree (yes/no): you enter not a yes or no
3、编写脚本filetype.sh
判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
完整脚本
[root@centos73 shell_scripts]# cat filetype.sh
#!/bin/bash
#Author=wang
RED="\033[31m" YELLOW="\033[0;33m" RESET="\033[0m" if [ $# -ne 1 ] ; then echo -e "$RED you must enter a parameter$RESET" exit 1 fi file=$1 type=`ls -ld $file |cut -c 1` #echo $type case $type in -) echo "general file" ;; d) echo "dir" ;; l) echo "link file" ;; *) echo "other" ;; esac
执行结果
[root@centos73 shell_scripts]# bash filetype.sh
you must enter a parameter
[root@centos73 shell_scripts]# bash filetype.sh /etc/passwd
general file
[root@centos73 shell_scripts]# ll /etc/passwd -rw-r--r--. 1 root root 2465 Jun 17 18:46 /etc/passwd [root@centos73 shell_scripts]# bash filetype.sh /etc dir [root@centos73 shell_scripts]# ll /etc/ -d drwxr-xr-x. 79 root root 8192 Jun 18 07:12 /etc/ [root@centos73 shell_scripts]# ll /etc/passwd -d -rw-r--r--. 1 root root 2465 Jun 17 18:46 /etc/passwd
[root@centos73 shell_scripts]# bash filetype.sh /bin/python
link file
[root@centos73 shell_scripts]# ll /bin/python
lrwxrwxrwx. 1 root root 7 Jan 9 13:55 /bin/python -> python2
4、编写脚本checkint.sh,判断用户输入的参数是否为正整数
完整脚本
[root@centos73 shell_scripts]# cat checkint.sh
#!/bin/bash
#Author=wang
RED="\033[31m" YELLOW="\033[0;33m" RESET="\033[0m" if [ $# -ne 1 ] ; then echo -e "$RED you must enter a parameter$RESET" exit 1 fi val=$1 int="^[0-9]+$" if [[ $val =~ $int ]] ; then echo "yes" else echo "no" fi
执行结果
[root@centos73 shell_scripts]# bash checkint.sh
you must enter a parameter
[root@centos73 shell_scripts]# bash checkint.sh 4
yes
[root@centos73 shell_scripts]# bash checkint.sh 4.5 no [root@centos73 shell_scripts]# bash checkint.sh 100 yes [root@centos73 shell_scripts]# bash checkint.sh 100.00000 no
示例
1判断年龄是年轻还是年老了
下面这个还不完善
#!/bin/bash
read -p " please input your age : " age
if [ $age -gt 18 ]; then echo " you are old " else echo " you are young " fi
2判断一下考试成绩(100分制)是不是优秀的
完整脚本
[root@centos73 shell_scripts]# cat score.sh
#!/bin/bash
#Author=wang
read -p "please input your score: " score if [[ ! "$score" =~ ^[0-9]+$ ]]; then #如果输入的不是任意数字 echo "please input your digits" elif [ "$score" -le 59 ];then echo "you are loser" elif [ "$score" -le 85 ];then echo "you are good" elif [ "$score" -le 100 ];then echo "you are excellent" else echo "other input error" fi
执行结果
[root@centos73 shell_scripts]# bash score.sh
please input your score: 34
you are loser
[root@centos73 shell_scripts]# bash score.sh please input your score: 59 you are loser [root@centos73 shell_scripts]# bash score.sh please input your score: 60 you are good [root@centos73 shell_scripts]# bash score.sh please input your score: 87 you are excellent [root@centos73 shell_scripts]# bash score.sh please input your score: 100 you are excellent
使用嵌套判断,if语句里面嵌套了if语句。但是这样易读性更差。没有特殊需求就不要嵌套了