getopt命令--用于shell脚本获取命令行选项
2018-02-05 17:51
441 查看
getopt命令
功能:主要用于shell脚本中,获取输入脚本的命令行参数
与此类似的还有getopts(bash内置的一个命令)
区别:getopt可以获取长选项参数,例如--example=123456789(长选项可以使用=号来附带参数值)这样的选项,同时,还可以获取-example,-test123456789(附带参数)这样的短选项,短选项不能使用=号来附带参数值。
getopts不能获取长选项。
getopt的经典使用是与set配合使用:set -- $(getopt xxxx),例如:set -- $(getopt -o ab:c:: --long along,blong:,clong:: -n 'example.sh' -- "$@")
set --命令将getopt获取的命令行结果赋予$1,$2...等内容。
通过这两个命令的相互组合,可以很轻松的处理shell脚本命令行选项。例如:
#!/bin/bash
#echo $@
#-o或--options选项后面接可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的
#-l或--long选项后面接可接受的长选项,用逗号分开,冒号的意义同短选项。
#-n选项后接选项解析错误时提示的脚本名字
ARGS=`getopt -o ab:c:: --long along,blong:,clong:: -n 'example.sh' -- "$@"`
if [ $? != 0 ]; then
echo "Terminating..."
exit 1
fi
#echo $ARGS
#将规范化后的命令行参数分配至位置参数($1,$2,...)
eval set -- "${ARGS}"
while true
do
case "$1" in
-a|--along)
echo "Option a";
shift
;;
-b|--blong)
echo "Option b, argument $2";
shift 2
;;
-c|--clong)
case "$2" in
"")
echo "Option c, no argument";
shift 2
;;
*)
echo "Option c, argument $2";
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
#处理剩余的参数
for arg in $@
do
echo "processing $arg"
done需要注意的是,像上面的-c选项,后面是可接可不接参数的,如果需要传递参数给-c选项,则必须使用如下的方式:
代码部分转载自http://blog.csdn.net/sofia1217/article/details/52244582
功能:主要用于shell脚本中,获取输入脚本的命令行参数
与此类似的还有getopts(bash内置的一个命令)
区别:getopt可以获取长选项参数,例如--example=123456789(长选项可以使用=号来附带参数值)这样的选项,同时,还可以获取-example,-test123456789(附带参数)这样的短选项,短选项不能使用=号来附带参数值。
getopts不能获取长选项。
getopt的经典使用是与set配合使用:set -- $(getopt xxxx),例如:set -- $(getopt -o ab:c:: --long along,blong:,clong:: -n 'example.sh' -- "$@")
set --命令将getopt获取的命令行结果赋予$1,$2...等内容。
通过这两个命令的相互组合,可以很轻松的处理shell脚本命令行选项。例如:
#!/bin/bash
#echo $@
#-o或--options选项后面接可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的
#-l或--long选项后面接可接受的长选项,用逗号分开,冒号的意义同短选项。
#-n选项后接选项解析错误时提示的脚本名字
ARGS=`getopt -o ab:c:: --long along,blong:,clong:: -n 'example.sh' -- "$@"`
if [ $? != 0 ]; then
echo "Terminating..."
exit 1
fi
#echo $ARGS
#将规范化后的命令行参数分配至位置参数($1,$2,...)
eval set -- "${ARGS}"
while true
do
case "$1" in
-a|--along)
echo "Option a";
shift
;;
-b|--blong)
echo "Option b, argument $2";
shift 2
;;
-c|--clong)
case "$2" in
"")
echo "Option c, no argument";
shift 2
;;
*)
echo "Option c, argument $2";
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
#处理剩余的参数
for arg in $@
do
echo "processing $arg"
done需要注意的是,像上面的-c选项,后面是可接可不接参数的,如果需要传递参数给-c选项,则必须使用如下的方式:
#./getopt.sh -b 123 -a -c456 file1 file2 Option b, argument 123 Option a Option c, argument 456 processing file1 processing file2 #./getopt.sh --blong 123 -a --clong=456 file1 file2 Option b, argument 123 Option a Option c, argument 456 processing file1 processing file2
代码部分转载自http://blog.csdn.net/sofia1217/article/details/52244582
相关文章推荐
- 使用getopt命令解析shell脚本的命令行选项
- 使用getopt命令解析shell脚本的命令行选项 【转】
- 使用getopt命令解析shell脚本的命令行选项
- 使用getopt命令解析shell脚本的命令行选项
- 使用getopts命令解析shell脚本的命令行选项
- Shell编程中的用户输入处理(4):在shell脚本中,使用read命令获取命令行输入
- linux通过awk命令,查询日志,分析数据,获取平均时间shell脚本
- shell脚本调用expect命令实现命令行自动交互
- shell 脚本之获取命令输出字符串以及函数参数传递
- dirname命令介绍-使用dirname在shell脚本中获取当前脚本文件所在的绝对路径
- shell 脚本中获取命令的输出
- Linux命令行与shell脚本编程-part1-基本bash shell命令
- win764位loadrunner安装问题:提示:少了Microsoft Visual c++2005 sp1运行时组件,安装时会提示命令行选项语法错误,键入“命令/?”可获取帮肋信息,无法正常安装;
- shell脚本命令 运行python文件&python命令行运行python代码
- 脚本乐园 Shell中命令行选项和参数的处理
- centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课
- linux C程序中获取shell脚本输出(如获取system命令输出)
- linux C程序中获取shell脚本输出(如获取system命令输出)
- shell脚本中如何获取命令的参数(2) ----处理命令参数
- vim独家秘技两条(1)在任意列中插入等差数列(非插件)(2)利用shell命令插入某个目录下的所有文件名(可用于cocos2d-x android的.mk脚本配置)(下)