码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2016-10-30 00:30:29      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:amp   exp   exists   deluser   exit   add   .com   userdel   --help   

练习一:

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

#!/bin/bash

#program:

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

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

PATH=/bin:www.3.qixoo.com/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命令练习:case选择结构语句练习

标签:amp   exp   exists   deluser   exit   add   .com   userdel   --help   

原文地址:http://www.cnblogs.com/qixoo/p/6011996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!