shell脚本学习——条件判断、流程控制

一、条件判断

     1、语法结构
           test 条件表达式
           [ 条件表达式 ]    两边需要有空格
           [[ 条件表达式 ]]   支持正则
     2、条件判断相关参数
        判断文件类型
           -e          判断文件是否存在
           -f           判断是否是一个文件
           -d          判断目录是否存在
           -L          判断是否是链接文件
           -b          判断文件是否为块设备
           -s          判断文件是否为非空,!-s是否为空
           -c          判断文件是否为字符设备文件
       判断文件权限
          -r           是否可读
          -w          是否可写
          -x           是否可执行
          -u           是否有suid,高级权限冒险位
          -g           是否sgid,高级权限强制位
          -k           是否有t位,高级权限粘滞位
       判断文件新旧
          -nt       file1 -nt file2      file1是否比file2新
          -ot       file1 -ot file2      file1是否比file2旧
          -ef       file1 -ef file2      file1与file2是否为同一文件
       判断整数
           -eq            相等
           -ne            不等
           -gt             大于
           -lt              小于
           -ge            大于等于
           -le             小于等于
       判断字符串
            -z           判断字符串是否为空,为0则成立
            -n           判断字符串是否为非空,不为0则成立
            string1 = string2       判断字符串是否相等
            string1 != string2       判断字符串是否不等
       多重条件判断
            -a 和 &&    逻辑与   
            -o 和 ||      逻辑或
            ;      用于分割命令或表达式
            &&前面为真执行后面的命令
            ||   前面为假执行后面的命令

    类C风格的数值比较
           ((   ))=表示赋值,==表示判断
           ((1<2));echo $?         >    !=    >=    <=
           字符串比较
             ===都表示判断
           [ "$a" = "$b" ]
           [ "$a" == "$b" ]
           [ "$a" != "$b" ]
      [ ][[  ]]的区别
      [ $A = hello ];echo $?  语法报错
      [ "$A" = "hello" ];echo $?  结果正常
      [[ $A = hello ]];echo $?  结果正常
      [ -e test1 && -L test1 ];echo $? 语法报错
      [[ -e test1 && -L test1 ]];echo $? 结果正常

二、流程控制语句
基本语法结构
(一)if结构

 if [ condition ];then
     command
     command
 fi
 
 [ 条件 ] && command

(二)if…else结构

     if [ condition ];then
             command1
         else
             command2
     fi

    [ 条件 ] && command1 || command2

eg 如果用户输入hello,打印world,否则打印“请输入hello”
#!/bin/env bash
read -p "请输入一个字符串:" str
if [ "$str" = "hello" ];then
    echo "world"
  else
    echo "请输入hello!"
fi

read -p "请输入一个字符串:" str;
[ "$str" = "hello" ] && echo "world" || echo "请输入hello!"

(三)if…elif…else结构

   if [ condition1 ];then
         command1
     elif [ condition2 ];then
         command2
     else
         command3
   fi   

(四)层层嵌套结构

 if [ condition1 ];then
      command1
      if [ condition2 ];then
         command2
      fi
 else
      if [ condition3 ];then
         command3
      elif [ condition4 ];then
         command4
      else
         command5
      fi
 fi

三、应用练习
1、判断主机是否可以通讯

  #!/bin/env bash
  # 判断当前主机与远程主机是否互通
  read -p "请输入需要远程的主机IP:" ip
  ping -c1 $ip &>/dev/null
  if [ $? -eq 0 ];then
     echo “当前主机与$ip是互通的”
   else 
     echo “当前主机与$ip不通”
  fi

2、判断一个进程是否存在

#!/bin/env bash
# 判断一个程序的进程是否存在
pgrep httpd &>/dev/null
if [ $? -ne 0 ];then
   echo "当前httpd进程不存在"
else
   echo "当前httpd进程存在"
fi
或者
test $? -eq 0 && echo "当前httpd进程存在" || echo "当前httpd进程不存在"

3、判断服务是否正常

#!/bin/env bash
# 判断网站服务是否正常
web=www.baidu.com
wget -P /file/ $web &>/dev/null
[ $? -eq 0 ] && echo "网站服务OK" && rm -f /file/index.* || echo "网站服务down"

eg
判断用户是否存在

#!/bin/env bash
read -p "请输入一个用户名:" user_name
id $user_name &>/dev/null
if [ $? -eq 0 ];then
    echo "用户存在"
else
    echo "用户不存在"
fi

你可能感兴趣的:(shell脚本学习)