zoukankan      html  css  js  c++  java
  • shell 脚本——第六节课 if判断语句

    if语句:
      if条件语句的使用格式:
      1、单分支语句
        if 条件;then
          执行语句
        fi
      

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


      3、多分支语句
        if 条件;then
          执行语句1
        elif;then
          执行语句2
        elif;then
          执行语句3
        else
          执行语句4
        fi

      退出码:
        exit
        在某些条件判断下,如果不满足该条件,我们必须手动退出程序,否则后面的代码无法执行;
        代码正确执行完成后,我们制定 exit 0 为正确退出码;

    1、判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.”


    2、给定一个用户,来判断这个用户是什么用户,如果是管理员用户,则显示“该用户为管理员”,否则显示“该用户为普通用户”

    3、判断某个文件是否存在
      #!/bin/bash
      # 判断文件是否存在
      if [ $# -lt 1 ]; then
        echo "At least one argument."
        exit 1
      fi

      if [ -e $1 ];then

        echo "存在"
      else
        echo "不存在"
      if

    4、判断当前系统上是否有用户的默认shell程序是否为bash程序,如果有,就显示有多少个这类用户,否则就显示没有这类用户;【并且显示出那些用户是bash】
      #!/bin/bash
      # 判断用户的默认shell程序类型

      declare -i sum=`grep "bin/bash$" /etc/passwd | wc -l`

        if grep "/bin/bash$" /etc/passwd &> /dev/null ; then

        echo "存在 $sum 个用户,shell程序为/bin/bash"
        grep "/bin/bash$" /etc/passwd | cut -d: -f1
        exit 0
      else
        echo "没有这类用户"
        exit 1
      fi

    5、写出一个脚本程序,给定一个文件,比如:/etc/inittab a、判断这个文件中是否有空白行? b、如果有,则显示其空白行的行号,否则显示没有空白行
      ^[[:space:]]*$

      #!/bin/bash
      #

      B=`grep -n "^[[:space:]]*$" /etc/inittab | wc -l`
      C=`grep -n "^[[:space:]]*$" /root/abc | cut -d: -f1`

      if [ $B -eq 0 ] ; then
        echo "没有空白行"
        exit 1
      else
        echo "有空白行,空白行为 $C 行"
        exit 0
      fi

    6、写一个脚本程序,给定一个用户,判断其UID与GID是否一样,如果一样,就显示该用户为“good guy”,否则显示为“bad guy”
      #!/bin/bash
      #

      1、for 做遍历 /etc/passwd
      for i in $(cat /etc/passwd);do //sed可以完成;
      2、判断每一行的UID和GID
      if [ `cut -d: -f3 $i` = `cut -d: -f4 $i` ];then
        echo "good guy"
        exit 0
      else
        echo "bad guy"
        exit 1
      fi
      done

    7、写一个脚本程序,给定一个用户,获取其密码警告期限($W);然后判断用户最近一次修改密码的时间距离今天是否已经小于警告期限;
      /etc/passwd /etc/shadow /etc/group /etc/gshadow
      用户(X) 密码 组 组密码
      #!/bin/bash
      #

    8、判断命令历史中历史命令的总条目是否大于1000,如果大于,则显示“some command will gone”,否则显示OK
      #!/bin/bash
      #
      S=`history | awk '{print $1}' | tail -1`

      if [ $S -gt 1000 ];then
        echo "some command will gone"
        exit 0
      else
        echo "OK"
      fi

    9、给定一个文件,如果是普通文件,就显示出来,如果是目录文件,也显示出来,否则就显示“无法识别”
      #!/bin/bash
      #
      # input()
      read -t 5 -p ("请输入一个文件:") filename // -t 等待时间
      echo # 默认用来换行

      if [ -n $filename ];then
        echo "eg. /etc/fstab"
        exit 8
      fi

      if [ -f $filename ]; then
        echo "$filename 是一个普通文件"
        exit 0
      elif [ -d $filename ];then
        echo "$filename 是一个目录文件"
        exit 0
      else
        echo "无法识别"
        exit 1
      fi

    ------------------------------------------------------------------------

    改成 case 语句;

    10、写一个脚本,能接受一个参数(文件路径),判断这个参数如果是一个存在的文件就显示“ok”,否则显示“No such file"
      #!/bin/bash
      #

      read -p "请输入一个文件路径:" filename

      if [ -e $filename ];then
        echo "OK"
      else
        echo "No such file"
      fi

    11、写一个脚本,给脚本传递两个参数,显示两则之和和两者之积
      #!/bin/bash
      #
      echo $[$1+$2]
      echo $[$1*$2]

  • 相关阅读:
    Python-第三方库requests详解
    python读取excel文件
    python 代理
    python urllib 和 urllib2
    HTTPS请求 SSL证书验证
    python requests.exceptions.ConnectionError
    python django -7 Git与项目
    python django -6 常用的第三方包或工具
    python django -5 进阶
    python django -4 模板
  • 原文地址:https://www.cnblogs.com/zwl123456/p/11441950.html
Copyright © 2011-2022 走看看