您的位置:首页 > 运维架构 > Shell

如何获取shell脚本的可选参数

2012-07-28 16:58 435 查看
直接上例子:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 脚本 file