shell指令碼-自定義option引數
阿新 • • 發佈:2019-01-29
想很久不知道怎麼命名這個題目,姑且就這樣吧,應該能表達我的意思了。
主要實現:
(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"
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
主要實現:
(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比較,就會報語法錯誤,因為少了一個"["。
根據這些引數,我們會有不同的執行結果,這裡只是簡單的把類名或方法名按指定的次數輸出顯示,具體執行方法視需求而定,過程是一樣的。
有什麼意見或建議大家可以提出
#!/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"
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