你怎么使用getopts的? [英] How do you use getopts?
本文介绍了你怎么使用getopts的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是最简单,最直接的,使用的方式 getopts的在庆典脚本。
如果我有一个名为脚本:的MyScript ,它的 CAN 取的参数: -p -r -s -x
如果参数则X退出
如果参数p然后回声端口10
如果参数s再加入2 + 2
如果参数,则成为包膜回声
这是一个假想的剧本,但我只是想看到如何做到这一点做了一个例子。
解决方案
而getopts的:xPSR寄存器选择;做
案例$选择在
X)退出;;
p)的呼应端口10 ;;
多个)((2 + 2));;
R)回声ENV ;;
\\? )回声$ {0 ## * /}[-xpsr];出口1 ;;
ESAC
DONE
what is the easiest, most straight forward, way to use getopts in bash script.
if i have a script called: myscript and it CAN take the the arguments: -p -r -s -x
if argument x then exit
if argument p then echo "port 10"
if argument s then add 2+2
if argument r then echo env
This is a hypothetical script but I would just like to see an example of how this would be done.
解决方案
while getopts :xpsr opt; do
case $opt in
x ) exit ;;
p ) echo port 10 ;;
s ) (( 2 + 2 )) ;;
r ) echo env ;;
\? ) echo "${0##*/}" [ -xpsr ]; exit 1 ;;
esac
done
这篇关于你怎么使用getopts的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文