linux命令练习:case选择结构语句练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux命令练习:case选择结构语句练习相关的知识,希望对你有一定的参考价值。
练习一:
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 ‘[email protected],@ @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 ‘[email protected],@ @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选择结构语句练习的主要内容,如果未能解决你的问题,请参考以下文章