shell script - the sixth lesson if the judge sentences

if statement:
  Use format if conditional statement:
  1, a single branch statements
    if condition; then
      execute the statement
    fi
  

  2, double branch statements
    if condition; then
      execute statement. 1
    the else
      execute the statement 2
    Fi


  3, multi-branch statements
    if condition; then
      execute the statement. 1
    elif; then
      execute the statement 2
    elif; then
      execute statement 3
    the else
      execute the statement. 4
    Fi

 

  Exit codes:
    Exit
    under certain conditions to determine if the condition is not satisfied, we have to manually exit the program, otherwise the code behind can not be executed;
    after the completion of the implementation of the code is correct, we have developed the correct exit code exit 0;

 

1, it is determined / etc / inittab file is greater than 100 lines, if yes, display the "/ etc / inittab is a big file." Displays whether the person "/ etc / inittab is a small file."


2, given a user, the user is to determine what the user if the user is an administrator, it will show "the user is an administrator". Otherwise, "the user is a regular user."

3, to determine whether a file exists
  # / bin / bash!
  Are # determine the file exists
  IF [$ # -lt 1]; the then
    echo "At Least One argument."
    Exit 1
  fi

  if [ -e $1 ];then

    echo "exist"
  the else
    echo "does not exist"
  IF

 

4, to determine whether a user's default shell bash whether the program, if there is, it shows how many of these users on the current system, otherwise showed no such users; [and show those users is bash]
  # /! bin / bash
  # determine the user's default shell type

  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]

Guess you like

Origin www.cnblogs.com/zwl123456/p/11441950.html