首页 > 代码库 > linux命令练习:case选择结构语句练习

linux命令练习:case选择结构语句练习

练习一:

  1、接收参数,提示输入的参数是什么类型:

#!/bin/bash

#program:

#接收参数并输出参数类型

#history donggen 2016-10-27-17:20

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bansh   

export PATH

  case $1 in 

     [[:digit:]])

         echo "it is Digit"

           ;;

      [[:lower:]])

          echo "it is Lower"

            ;;

       [[:upper:]])

          echo "it is Upper"

            ;;

        *)

          echo "it is Unknown"

            ;;

   esac


练习二:

   2、写一个脚本,可以接受选项及参数,而后能获取每一个选项,及选项的参数;

并能根据选项及参数做出特定的操作。比如administrators.sh -add tom,jerry --del tom,blair

-v|--verbose -h|--help

  #!/bin/bash

  #program:

  #练习接收参数,并获取参数

  #history  donggen 2016-10-27-22:40

  PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

  export PATH

  DEBUG=0

  ADD=0

  DEL=0 


for I in `seq 0 $#`; do

  if [ $# -gt 0 ]; then

         case $1 in

         -v|--verbose)

              DEBUG=1

              shift ;;

         -h|--help)

              echo "Usage:`basename $0` --add USER_LIST --del USER_LIST -v|--verbose -h|--help"

              exit 0

              ;;

         --add)

              ADD=1

              ADDUSERS=$2

              shift 2

              ;;

         --del)

              DEL=1

              DELUSERS=$2

              shift 2

              ;;

         *)

              echo "Usage:`basename $0` --add USER_LIST --del USER_LIST -v|--verbose -h|--help"

              exit 7

              ;;

    esac

  fi

done

 

if [ $ADD -eq 1 ]; then

  for USER in `echo $ADDUSERS |sed ‘s@,@ @g‘`; do

    if id $USER &>/dev/null; then

      [ $DEBUG -eq 1 ] &&echo "$USER exists."

    else

      useradd $USER

      [ $DEBUG -eq 1 ] &&echo "Add user $USER finished."

    fi

  done

fi

 

if [ $DEL -eq 1 ]; then

  for USER in `echo $DELUSERS |sed ‘s@,@ @g‘`; do

    if id $USER &>/dev/null; then

      userdel -r $USER

      [ $DEBUG -eq 1 ] &&echo "Delete $USER finished."

    else

      [ $DEBUG -eq 1 ] &&echo "$USER not exist."

    fi

  done

fi

  

  





本文出自 “学linux历程” 博客,请务必保留此出处http://woyaoxuelinux.blog.51cto.com/5663865/1866544

linux命令练习:case选择结构语句练习