直接上例子:
cat option_sh
#!/bin/bash
echo $*
# get the optional arguments
while getopts f:t: value
do
case $value in
f )
RHOST_INFO=$OPTARG # 从$OPTARG中获得参数值
;;
t )
TIMEOUT=$OPTARG
;;
esac
done
echo $@
shift $(($OPTIND-1)) # 偏移至后面的参数
echo $*
echo $1
if [ $TIMEOUT == "30" ] # 获得的参数为字符串
then
echo here
fi
ngnlinux1 [** NONE **]/home/qius $
ngnlinux1 [** NONE **]/home/qius $ ./option_sh -t 30 -f ss other paras
-t 30 -f ss other paras
-t 30 -f ss other paras
other paras
other
here
使用方法:example.sh -f file_name -t timeoutvalue other paras