1
我有一個bash腳本myscript.sh
。 我的意思是從其中調用另一個腳本,命令或內置函數,例如diff
。 我的意思是將選項傳遞給myscript.sh
,其中一些在調用時會傳遞給diff
。將在bash腳本中收到的選項傳遞給被調用的腳本,命令或內置
我實現了這是通過getopt
設置選項字符串optstring
,然後使用
eval "diff ${optstring} ${file} ${TRG_DIR}/${filebase2}"
到目前爲止,它的工作,但我不知道這是容易傳遞參數時,問題的方式與通配符等無論如何,...
有沒有更好的方式來做到這一點?
我成立
optstring
的方式是
set -o errexit -o noclobber -o nounset -o pipefail
params="$(getopt -o qy --long brief,side-by-side,suppress-common-lines --name "$0" -- "[email protected]")"
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
echo params=$params
echo [email protected]
eval set -- "$params"
optstring=""
# These variables are likely not needed
brief=false
sbs=false
scl=false
#while false ; do
while true ; do
case "$1" in
-q|--brief)
optstring=${optstring}" -q"
brief=true
echo "brief"
shift
;;
-y|--side-by-side)
optstring=${optstring}" -y"
sbs=true
echo "side-by-side"
shift
;;
--suppress-common-lines)
optstring=${optstring}" --suppress-common-lines"
scl=true
echo "suppress-common-lines"
shift
;;
--)
shift
break
;;
*)
echo "Not implemented: $1" >&2
exit 1
;;
esac
done
echo optstring=${optstring}
我發現,當沒有選擇,我得到的錯誤'選項[@]:綁定variable'。我怎樣才能擺脫它? –
https://stackoverflow.com/questions/7577052/bash-empty-array-expansion-with-set-u解決了這個問題。 –