linux脚本中option,shell脚本-自定义option参数

本文介绍了一个bash shell脚本,它可以根据传入的不同参数执行不同的功能,如ls-l和ls-a。脚本支持提示选择、读取参数并提供帮助信息。核心内容包括使用select、shift和条件判断来控制运行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想很久不知道怎么命名这个题目,姑且就这样吧,应该能表达我的意思了。

主要实现:

(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比较,就会报语法错误,因为少了一个"["。

根据这些参数,我们会有不同的运行结果,这里只是简单的把类名或方法名按指定的次数输出显示,具体运行方法视需求而定,过程是一样的。

有什么意见或建议大家可以提出

0818b9ca8b590ca3270a3433284dd417.png 以下是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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值