if语句

Posted wang618

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了if语句相关的知识,希望对你有一定的参考价值。

 

 

 

 

一条件选择语句——if语句

 

 

对于if条件语句,简单地说,其语义类似于如果…那么。
if条件语句是Linux运维人员在实际生产工作中使用得最频繁也是最重要的语句。
 

 

 

(一)if条件语句的语法

1.单分支结构
if <条件表达式>then
指令
fi
 
 
if <条件表达式>; then指令
fi
上文的“<条件表达式>”部分可以是test、[ ]、[[ ]]、(())等条件表达式,甚至可以直接使用命令作为条件表达式。
每个if条件语句都以if开头,并带有then,最后以fi结尾。
 

 

技术图片

 

 

 

 

 

 


2双分支结构

if条件语句的双分支结构主体为“如果…,那么.,否则…。
条件语句的双分支结构语法为:
if <条件表达式>then
指令集1,条件为真的分支代码
else
指令集2,条件为假的分支代码
fi
 
 
 

技术图片

 

 

 

 

 

 

 

3多分支结构

if条件语句多分支结构的主体为“如果…,那么…,否则如果…,那么,否则如果.,那么., 否则…。
if条件语句多分支语法为:
if 判断条件 1 ; then

条件为真的分支代码
elif 判断条件 2 ; then
条件为真的分支代码
elif 判断条件 3 ; then
条件为真的分支代码
else
以上条件都为假的分支代码
fi
逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

 

 

 技术图片

 

 

 

 

 

 

多分支的if语句和默认路由是一样的,网络都不匹配的情况下走默认路由。

[[email protected] ~]# type  if
if is a shell keyword
[[email protected] ~]# 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号等信息

完整脚本

[[email protected] 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

 

 

 

 

执行结果

[[email protected] shell_scripts]# bash   createuser.sh   wu
wu has existed !
uid=1023(wu) gid=1023(wu) groups=1023(wu)
[[email protected] shell_scripts]# bash   createuser.sh   wang
wang has existed !
uid=1022(wang) gid=1022(wang) groups=1022(wang)
[[email protected] shell_scripts]# bash   createuser.sh   xixixi
uid=1025(xixixi) gid=1025(xixixi) groups=1025(xixixi)

 

 

 

 

升级版,显示颜色

[[email protected] 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,或是其它信息

完整脚本

[[email protected] 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

 

 

 

 

 

 

 执行结果

[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):yes
you enter yes
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):YES
you enter yes
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):Yes    
you enter yes
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):YEs
you enter yes

 

 

[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):NO
you enter no 
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):no
you enter no 
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):No
you enter no 
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):nO
you enter no 

 

 

 

[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):df
you enter not a yes or no
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):1234f
you enter not a yes or no
[[email protected] shell_scripts]# bash  yesorno.sh
do you agree (yes/no):
you enter not a yes or no

 

 

 

 

 

 

3、编写脚本filetype.sh

判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

 完整脚本

[[email protected] 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

 

 

 

 

 执行结果

技术图片

 

 

 

[[email protected] shell_scripts]# bash   filetype.sh
 you must enter a parameter

 

 

 

[[email protected] shell_scripts]# bash   filetype.sh   /etc/passwd
general file
[[email protected] shell_scripts]# ll   /etc/passwd
-rw-r--r--. 1 root root 2465 Jun 17 18:46 /etc/passwd
[[email protected] shell_scripts]# bash   filetype.sh   /etc
dir
[[email protected] shell_scripts]# ll /etc/ -d
drwxr-xr-x. 79 root root 8192 Jun 18 07:12 /etc/
[[email protected] shell_scripts]# ll   /etc/passwd -d
-rw-r--r--. 1 root root 2465 Jun 17 18:46 /etc/passwd

 

 

 

 

[[email protected] shell_scripts]# bash   filetype.sh   /bin/python
link file
[[email protected] shell_scripts]# ll  /bin/python
lrwxrwxrwx. 1 root root 7 Jan  9 13:55 /bin/python -> python2

 

 

 

 

 

 

4、编写脚本checkint.sh,判断用户输入的参数是否为正整数

 完整脚本

[[email protected] 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

 

 

 

 

 

 执行结果

[[email protected] shell_scripts]# bash   checkint.sh
 you must enter a parameter
[[email protected] shell_scripts]# bash   checkint.sh  4
yes
[[email protected] shell_scripts]# bash   checkint.sh  4.5
no
[[email protected] shell_scripts]# bash   checkint.sh  100
yes
[[email protected] 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分制)是不是优秀的

完整脚本

[[email protected] 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

 

 

 

 

执行结果

[[email protected] shell_scripts]# bash score.sh 
please input your score: 34
you are loser
[[email protected] shell_scripts]# bash score.sh 
please input your score: 59
you are loser
[[email protected] shell_scripts]# bash score.sh 
please input your score: 60
you are good
[[email protected] shell_scripts]# bash score.sh 
please input your score: 87
you are excellent
[[email protected] shell_scripts]# bash score.sh 
please input your score: 100
you are excellent

 

 

 

 

 

 

使用嵌套判断,if语句里面嵌套了if语句。但是这样易读性更差。没有特殊需求就不要嵌套了

 

 技术图片

 

以上是关于if语句的主要内容,如果未能解决你的问题,请参考以下文章

for+if语句 和 仅用if语句 的程序效率

两个if语句可以平行使用吗

python流程控制语句-if语句

java if语句

R语言中的if else语句

python语句结构(if判断语句)