shell系列------迴圈解析指令碼的引數
阿新 • • 發佈:2019-01-27
useage() {
echo "useage : monkey_test [--case # <test case #>]"
echo " [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]"
echo " [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]"
echo " [-t <seconds>]"
echo " [--seed # <seed #>]"
echo " [--package-list <test case package list>]"
echo " [--phonenumber # <fixed dial #>]"
echo " [--IMETest]"
echo " [--version]"
echo " [-h]"
exit 0
}
#parsing the argument
while [ $# != 0 ]; do #引數個數不等於0
flag="$1"
case "$flag" in
-h) useage;
;;
-t) if [ $# -gt 1 ]; then #add the package argument #引數個數大於1
arg3="$2"
#Set the CountDwon timer
countdowntimer="$arg3"
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
-p) if [ $# -gt 1 ]; then #add the package argument and install the empty Activity apk
arg3="$2"
package="$package -p $arg3" #
ispackage="true"
shift #傳入的引數左移一個,$2變成了$1
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
-c) if [ $# -gt 1 ]; then #add the category argument
arg="$2"
category="$category -c $arg"
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--seed) if [ $# -gt 1 ]; then
arg1="$2"
seed=$arg1
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--case) if [ $# -gt 1 ]; then
pm install /system/scripts/monkey/EmptyActivity.obj
arg2="$2"
echo "Choose Test Case : $arg2"
test_case_package=`cat $package_list | head -n $arg2 | tail -n 1 | awk '{print $2}'| tr -d '\r\n'`
#ToDo: modify to get more argument
#test_case_category=`cat $package_list | head -n $arg2 | tail -n 1 | awk '{print NF}'`
package=" -p $test_case_package"
echo "package : $package"
echo "category : $category"
run=259200
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--package-list) awk '{print $1 " " $3}' $package_list
exit 0
;;
--version) echo "monkey script version $version"
exit 0
;;
--phonenumber) if [ $# -gt 1 ]; then
arg3="$2"
phonenumber="$arg3"
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--IMETest)
pm install /system/scripts/monkey/TestIME.obj
package="$package -p com.android.testime"
;;
--w)
argument="--pkg-whitelist-file $2"
shift
;;
--bl)
argument="--pkg-blacklist-file $2"
shift
;;
*) echo "UnKnown: argument : $flag"
useage;
;;
esac
shift
done
echo "useage : monkey_test [--case # <test case #>]"
echo " [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]"
echo " [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]"
echo " [-t <seconds>]"
echo " [--seed # <seed #>]"
echo " [--package-list <test case package list>]"
echo " [--phonenumber # <fixed dial #>]"
echo " [--IMETest]"
echo " [--version]"
echo " [-h]"
exit 0
}
#parsing the argument
while [ $# != 0 ]; do #引數個數不等於0
flag="$1"
case "$flag" in
-h) useage;
;;
-t) if [ $# -gt 1 ]; then #add the package argument #引數個數大於1
arg3="$2"
#Set the CountDwon timer
countdowntimer="$arg3"
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
-p) if [ $# -gt 1 ]; then #add the package argument and install the empty Activity apk
arg3="$2"
package="$package -p $arg3" #
ispackage="true"
shift #傳入的引數左移一個,$2變成了$1
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
-c) if [ $# -gt 1 ]; then #add the category argument
arg="$2"
category="$category -c $arg"
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--seed) if [ $# -gt 1 ]; then
arg1="$2"
seed=$arg1
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--case) if [ $# -gt 1 ]; then
pm install /system/scripts/monkey/EmptyActivity.obj
arg2="$2"
echo "Choose Test Case : $arg2"
test_case_package=`cat $package_list | head -n $arg2 | tail -n 1 | awk '{print $2}'| tr -d '\r\n'`
#ToDo: modify to get more argument
#test_case_category=`cat $package_list | head -n $arg2 | tail -n 1 | awk '{print NF}'`
package=" -p $test_case_package"
echo "package : $package"
echo "category : $category"
run=259200
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--package-list) awk '{print $1 " " $3}' $package_list
exit 0
;;
--version) echo "monkey script version $version"
exit 0
;;
--phonenumber) if [ $# -gt 1 ]; then
arg3="$2"
phonenumber="$arg3"
shift
else
echo "UnKnown: argument : $flag"
useage;
fi
;;
--IMETest)
pm install /system/scripts/monkey/TestIME.obj
package="$package -p com.android.testime"
;;
--w)
argument="--pkg-whitelist-file $2"
shift
;;
--bl)
argument="--pkg-blacklist-file $2"
shift
;;
*) echo "UnKnown: argument : $flag"
useage;
;;
esac
shift
done