bash 编程 if

 

面向过程
          顺序执行
          条件判断     if  case
          循环执行     for while  until

条件判断     
[ 表达式 ]   表达式左右有空格
test  表达式
命令

变量
          本地变量   local 声明  
          局部变量
          环境变量-全局变量
          位置变量   $1  $2
          特殊变量   $?   命令返回结果     $@  全部参数      $# 参数个数

if
          if 条件 ;then
                         语句1
                         语句2
          fi
                              例题1  写一个脚本,实现如下功能:     如果用户存在,就说明其存在
                                        #!/bin/bash
                                        #
                                        UserName=user1
                                        if grep "^$UserName\>" /etc/passwd &> /dev/null; then
                                                    echo "$UserName exists."
                                        fi
                                        #!/bin/bash
                                        UserName=user1
                                        if id $UserName &> /dev/null; then
                                                     echo "$UserName exists."
                                        fi

          双分支语句
          if  条件;then
                         语句1
                         语句2
          else     
                         语句1
                         语句2
          fi

          数字测试           数值1  比较符号  数值2
                                             小于     -lt
                                             小于等于     -le
                                             大于     -gt
                                             大于等于     -ge
                                             等于     -eq
                                             不等于          -ne

例子:写一个脚本,生成两个随机数,比较其大小;显示大数;     bash有个内置变量:$RANDOM
                                                                      #!/bin/bash
                                                                      #
                                                                      A=$RANDOM
                                                                      B=$RANDOM

                                                                      if [ $A -ge $B ]; then
                                                                                       echo "Max number is $A."
                                                                      else
                                                                                       echo "Max number is $B."
                                                                      fi
练习:给定一个用户,如果其ID号大于499,就说明其是普通用户,否则,就说明其是管理员或系统用户;
                                                                        #!/bin/bash
#
UserName=daemon

Uid=`id -u $UserName`

if [ $Uid -gt 499 ]; then
  echo "A common user: $UserName."
else
  echo "admin user or system user: $UserName."
fi   
               字符测试

                             等于  ==     
                             不等于 !=
                              -z  string     string 为空 ture
                              -n  string    string 不为空  ture
                                                   #!/bin/bash
                                                   #
                                                   Shell=`grep "^$1:" /etc/passwd | cut -d: -f7`
                                                   if [ -z $Shell ]; then
                                                             echo "No such user or User's shell is null."
                                                             exit 10
                                                   fi
                                                   if [ "$Shell" == "/bin/bash" ]; then
                                                                 echo "Bash User."
                                                                    Ret=0
                                                         else
                                                                  echo "Not Bash User."
                                                                     Ret=9
                                                          fi
                                                                      exit $Ret      
               组合条件测试
                                   -a  与  [ 条件1 -a  条件2 ]     
                                   -o 或
                                   -i  非
                                   一般用在一个[]内

                                   && 与 [ 条件1 ] && [ 条件2 ]
                                   ||   或

              多重判断
                                   if 条件 ;then
                                                  语句
                                   elif 条件;then
                                                  语句
                                   else 
                                                   语句
                                   fi

               文件测试
                                   -f  是否普通文件
                                   -d 是否目录文件
                                   -e 文件是否存在
                                   -r 对当前用户是否有可读权限
                                   -w对当前用户是否有可写权限
                                   -x 对当前用户是否有可执行权限
                                   -s 文件是否存在且非空
               cmd1 && cmd2   命令1执行正确,开始执行命令2
               cmd1 ||   cmd2   命令1执行不正确,开始执行命令2

      cmd1 ||   cmd2  &&  cmd3  命令1执行正确 直接执行命令3 ,
                                               命令1执行不正确,执行命令2 ,如命令2 执行正确,再执行命令3

            case语句
                            case   变量  in
                                        value1 )
                                                    语句
                                                    ;;
                                        value2 )
                                                    语句
                                                    ;;
                                          * )
                                                    语句
                                                        ;;
                               esac

                例子:写一个脚本
                            1、显示如下菜单给用户:
                                    m|M) show memory usages;
                                    d|D) show disk usages;
                                    q|Q) quit
                            2、  如果用户选择了第一项,则显示内存使用信息;
                                   如果选择了第二项,则显示磁盘挂载及使用相关信息;
                                   如果是第三项,退出,并显示选择退出;
                                   其它任何内容,均说明错误选项;

                #!/bin/bash
                cat << EOF 
                m|M) show memory usages;


                d|D) show disk usages;
                q|Q) quit
                EOF

                read -p "Your choice: " Choice

                case $Choice in
                                    m|M)
                                             free -m ;;
                                    d|D)
                                             df -lh ;;
                                    q|Q)
                                              echo "Quit..."
                                              exit 0
                                               ;;
                                        *)
                                               echo "Invalid input."
                                               exit 5
                                                ;;
                   esac

 

你可能感兴趣的:(bash)