if 语句-条件选择
-
if 是一个shell的关键字
1[root@Centos6app]#type if 2if is a shell keywordif 选择执行
if 可以嵌套使用 -
用法
1if COMMANDS; then 2 COMMANDS; 3[ elif COMMANDS; then 4 COMMANDS; ] 5 ... 6[ else COMMANDS; ] 7fi 8 9执行 “if COMMANDS” 列表。 如果它的退出状态是零,那么执行“然后命令”列表。 10否则,依次执行每个“elif COMMANDS”列表,如果其退出状态为零,则执行相应的“then COMMANDS”列表,然后执行if命令。 11否则,执行“else COMMANDS”列表(如果存在)。 12整个结构的退出状态是所执行的最后一个命令的退出状态,如果条件不成立,则为零。 13 14if 支持单分支、双分支、多分支用法: 15if 判断条件1 ; then 16 条件为真的分支代码 17elif 判断条件2; then 18 条件为真的分支代码 19elif 判断条件3; then 20 条件为真的分支代码 21else 22以上条件都为假的分支代码 23fi 24逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句 -
示例
1根据命令的退出状态来执行命令 2if ping -c1 -W2 station1 &> /dev/null ; then 3 echo 'Station1 is UP' 4elif grep "station1" ~/maintenance.txt &> /dev/null ; then 5 echo 'Station1 is undergoing maintenance‘ 6else 7 echo 'Station1 is unexpectedly DOWN!' 8 exit 1 9fi 10 11判断年龄----->最简单的示例 12read -p "Please input your age: " age 13if [[ ! "$age" =~ ^[0-9]+$ ]] ; then 14 echo "please input right number!" 15 exit 2 16elif [ "$age" -le 18 ] ; then 17 echo "you are a child" 18elif [ "$age" -le 50 ] ; then 19 echo "you are very young" 20elif [ "$age" -le 80 ] ; then 21 echo "you can go home" 22elif [ "$age" -le 120 ] ; then 23 echo "you need to feel life" 24else 25 echo "Go to hell" 26fi 27 28if嵌套最简单示例 29read -p "Please input your score: " score 30if [[ "$score" =~ ^[0-9]+$ ]]; then --------->注意书写时 变量score 上下统一,**别上下写的不统一**,就容易报错 31 if [ "$score" -le 59 ] ; then 32 echo "your score is so bad" 33 elif [ "$score" -le 70 ] ; then 34 echo "your score is good" 35 elif [ "$score" -le 100 ] ; then 36 echo "unbeliveable" 37 else 38 echo "Go to hell" 39 fi 40else 41 echo "Please input correct score!" 42fi -
练习题
1、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息 #!/bin/bash if id $1 &> /dev/null ; then echo "$1 User already exists!" else useradd $1 > /dev/null && echo "$1 User created successfully,$1 info
id $1" fi 如果不用if语句,id $1 &> /dev/null && echo "$1 user already exists!" || useradd $1 ; echo "User created successfully,$1 infoid $1"2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no, 并判断用户输入的是yes还是no,或是其它信息 #!/bin/bash read -p "Do you agree? (yes or no): " ans if [[ "$ans" =~ ^Yy?$ ]] ; then echo "Ok" elif [[ "$ans" =~ ^[Nn][Oo]?$ ]] ; then echo "hehe" else echo "Input error" fi
3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型) #!/bin/bash read -p "Please enter the file name: " file if [ -f "$file" ] ; then echo "The $file is ordinary documents" elif [ -d "$file" ] ; then echo "The $file is table of contents" elif [ -L "$file" ] ; then echo "The $file is link file" else echo "The $file is othe types" fi
4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数 #!/bin/bash 1、 read -p "Please input a number: " num if [[ "$num" =~ ^[[:digit:]]+$ ]] ; then if [[ "$num" -gt 0 ]] ; then echo "Entered correctly!" else echo "Input error!" fi else echo "Not a positive integer" fi 2、 read -p "please input :" nub if [[ $arg =~ ^[[:digit:]]+$ ]] && [ $(expr $arg + 0) != 0 ] ; then echo "Bingo!" else echo "Wrong!" fi
case 语句-条件判断
1[root@Centos6app]#type case 2case is a shell keyword
-
用法
1case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac 2case 变量引用 in 3PAT1) 4 分支1 5 ;; 6PAT2) 7 分支2 8 ;; 9... 10*) 11 默认分支 12 ;; 13esac -
case支持glob风格的通配符:
1*: 任意长度任意字符 2?: 任意单个字符 3[]:指定范围内的任意单个字符 4a|b: a或b 5 6最简单示例 7yes or no 8#!/bin/bash 9read -p "Do you agree? (yes or no): " ans 10case $ans in 11[Yy]|[Yy][Ee][Ss]) 12 echo "Ok" 13 ;; 14[Nn]|[Nn][Oo]) 15 echo "hehe" 16 ;; 17*) 18 echo "What do you want to do !" 19 ;; 20esac