1
我想加入並將腳本的其餘參數發送給命令。例如:從arg 3開始加入bash剩餘參數
if [ "$2" == "exec" ]; then
other_script execute "$3 $4 $5 $6 $7 (etc etc etc)"
fi
請指教。
我想加入並將腳本的其餘參數發送給命令。例如:從arg 3開始加入bash剩餘參數
if [ "$2" == "exec" ]; then
other_script execute "$3 $4 $5 $6 $7 (etc etc etc)"
fi
請指教。
一種方法是使用bash
array slice notation:
foo() {
echo "[$1]"
echo "[$2]"
echo "[${@:3}]"
}
產地:
$ foo a b c d ef
[a]
[b]
[c d ef]
,你會在你的代碼實現爲:
if [ "$2" == "exec" ]; then
other_script execute "${@:3}"
fi
如果你需要說的第3和第第四個參數,您可以將長度應用於切片:
other_script execute "${@:3:2}" # :2 is a length specification
另一種方式,如果你並不需要爭論$1
或$2
不再是僅僅將它們轉移出來的樣子:
foo=${1:?Missing argument one}
bar=${2:-Default}
shift 2
echo "[email protected]" # the first two args are gone, so this is now args #3 on
我更喜歡這種方式,說實話,一對夫婦的原因:
我真的很喜歡這個偉大的答案的細節! – Martlark