想很久不知道怎么命名这个题目,姑且就这样吧,应该能表达我的意思了。
主要实现:
(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 <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