bash命令行参数 [英] bash command line arguments
本文介绍了bash命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要写一个bash脚本这需要不同的参数。它应该被用来像正常的Linux控制台程序:
I want to write a bash script which takes different arguments. It should be used like normal linux console programs:
my_bash_script -p 2 -l 5 -t 20
因此,值2应保存在变量称为页和参数L应保存在变量称为长度和20应当被保存在一个可变时间的值。
So the value 2 should be saved in a variable called pages and the parameter l should be saved in a variable called length and the value 20 should be saved in a variable time.
什么是做到这一点的最好方法是什么?
What is the best way to do this?
推荐答案
使用 getopts的
-builtin结果
这里有一个教程
Use the getopts
-builtin
here's a tutorial
pages= length= time=
while getopts p:l:t: opt; do
case $opt in
p)
pages=$OPTARG
;;
l)
length=$OPTARG
;;
t)
time=$OPTARG
;;
esac
done
shift $((OPTIND - 1))
移$((OPTIND - 1))
转移的命令行参数,使您可以访问可能的参数给脚本,即 $ 1, $ 2,...
shift $((OPTIND - 1))
shifts the command line parameters so that you can access possible arguments to your script, i.e. $1, $2, ...
这篇关于bash命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文