我正在寫一個bash腳本這將輸入參數的名稱使用一個變量的值,該命令是這樣的:在爭吵
command -a -b -c file -d -e
我想檢測一個特定的參數( -b)與它的特定的位置($ 1,$ 2,$ 3)
#! /bin/bash
counter=0
while [ counter -lt $# ]
do
if [ $($counter) == "-b" ]
then
found=$counter
fi
let counter+=1
done
問題上升$($counter)
。有沒有辦法使用$counter
來調用參數的值?例如,如果counter=2
,我想調用參數$2
的值。 $($counter)
不起作用。
使用'getopts'。例如http://stackoverflow.com/a/14203146/1983854可以幫助。 – fedorqui
好的謝謝!我會研究它,但如果我想要以下文件名,例如-b文件名,「getopts」是否也提供此功能? –
是的。你看過[documentation](http://linux.die.net/man/1/getopt)嗎? –