shell脚本-⾃定义option参数
想很久不知道怎么命名这个题⽬,姑且就这样吧,应该能表达我的意思了。
主要实现:
(1)shell命令根据不同的option返回不同执⾏的功能,如ls -l和ls -a会输出不同结果。
(2)在运⾏过程中可以读取参数
(3)“提⽰选择”,罗列所有可选项,让调⽤者选择
(4)适当地给出“help”帮助信息
这⾥先说明⼏点
(1)select:选择提⽰使⽤的是bash中的扩展改功能select,linux下默认是bash,unix中就必须显⽰指定"#!/bin/bash"
(2)shift:⽤于把参数逐个处理掉,shfit n 把前⾯n个参数去掉,后⾯参数前移
(3)[expr]和[[expr]]:若能保证表达式中变量不会为空,两者效果都是⼀样的,但是如果有变量为空,如[$1=$className ]中$1为空,结果就是[=$className ],是“["和$className⽐较,就会报语法错误,因为少了⼀个"["。
根据这些参数,我们会有不同的运⾏结果,这⾥只是简单的把类名或⽅法名按指定的次数输出显⽰,具体运⾏⽅法视需求⽽定,过程是⼀样的。
有什么意见或建议⼤家可以提出
以下是dis.sh
#!/bin/bash
classNames="Calendar DeskClock Note FileExplorer"
methodNames="CalendarMethod DeskClockMethod NoteMethod FileExplorerMethod"
#the running type,0 for class,1 for method,defalut -1
type=-1
#the runtest command
#commandTmp="adb shell uiautomator runtest SanityAccessoryTestCase.jar"
commandTmp="echo"
help()
{
cat <<HELP
NAME:dis - display the gived className or methodName
USAGE:dis [-c className] [-c] [-m methodName] [-m] [-n num] [-h ]
ExAMPLE:dis -c Calendar -n 3
dis -m CalendarMethod -n 3
dis -c
dis -h
dis
NOTE:can not use -c and -m at the same time!
HELP
exit 0
}
testAll()
{
echo $classNames $methodNames
exit 0
}
#list and choose the name for testing
listClassName()
{
echo "input the className"
select className in $classNames;do
break
done
}
#list and choose the name for testing
listMethodName()
{
echo "input the methodName"shell最简单脚本
select methodName in $methodNames;do
break
done
}
#default run all testCase for one time
if [ -z "$1" ];then
testAll
else
while [ -n "$1" ];do
case $1 in
-
h)help;;
-c)
if [[ $type -eq 1 ]];then
echo "can not set option '-c' and '-m' at the same time'";exit 1                fi
type=0
# no className
if [ -z "$2" ];then
listClassName
echo "input the frequency of running(>0):"
read runFrequency;shift 1
else
className=$2;shift 2
fi;;
-n)
if [ -z "$2" ];then
echo "must appoint the frequency of running after '-n'";exit 1                  else
runFrequency=$2;shift 2
fi;;
-m)
if [[ $type -eq 0 ]];then
echo "can not set option '-c' and '-m' at the same time'";exit 1                fi
type=1
# no methodName
if [ -z "$2" ];then
listMethodName
echo "input the frequency of running(>0):"
read runFrequency;shift 1
else
methodName=$2;shift 2
fi;;
*)echo "error:no such potion $1.try 'dis -h' for more information";exit 2;;
esac
done
#className or methodName is valid? 0 for valid,1 for invalid
isValid=1
#if test class
if [[ $type -eq 0 ]];then
for var in $classNames;do
if [[ $className = $var ]];then
isValid=0
break
fi
done
if [[ runFrequency -gt 0 ]];then
while [[ $runFrequency -ne 0 ]];do
commandTmp=$commandTmp" $className"
#runFrequency=runFrequency-1
runFrequency=`expr $runFrequency "-" 1`
done
else
echo "the frequency of running must >0 ";exit 3
fi
#if test method
elif [[ $type -eq 1 ]];then
for var in $methodNames;do
if [[ $methodName = $var ]];then
isValid=0
break
fi
done
if [[ runFrequency -gt 0 ]];then
while [[ $runFrequency -ne 0 ]];do
commandTmp=$commandTmp" $methodName"
#runFrequency=runFrequency-1
runFrequency=`expr $runFrequency "-" 1`
done
else
echo "the frequency of running must >0 ";exit 3
fi
else
echo "you need to appoint the test type,try 'dis -h' for more information";exit 4
fi
if [[ $isValid -eq 0 ]];then
eval $commandTmp
else
echo "className or methodName is invalid,try 'dis -h' for more information";exit 4        fi
fi

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。