我正在編寫一個很是簡單的腳本,該腳本將調用另外一個腳本,而且須要將參數從當前腳本傳播到正在執行的腳本。 java
例如,個人腳本名稱是foo.sh
並調用bar.sh
bash
foo.sh: ssh
bar $1 $2 $3 $4
如何在不顯式指定每一個參數的狀況下執行此操做? 測試
#!/usr/bin/env bash while [ "$1" != "" ]; do echo "Received: ${1}" && shift; done;
只是認爲這在嘗試測試args如何進入腳本時可能會更有用 spa
個人SUN Unix有不少限制,即便「 $ @」也沒法按需解釋。 個人解決方法是$ {@}。 例如, code
#!/bin/ksh find ./ -type f | xargs grep "${@}"
順便說一句,我必須有這個特定的腳本,由於個人Unix也不支持grep -r server
對於bash和其餘相似Bourne的外殼: it
java com.myserver.Program "$@"
正常工做,除非您有空格或轉義字符。 在這種狀況下,我找不到捕獲參數並將其發送到腳本內的ssh的方法。 test
這多是有用的,可是很是醜陋 awk
_command_opts=$( echo "$@" | awk -F\- 'BEGIN { OFS=" -" } { for (i=2;i<=NF;i++) { gsub(/^[a-z] /,"&@",$i) ; gsub(/ $/,"",$i );gsub (/$/,"@",$i) }; print $0 }' | tr '@' \' )
我知道這個問題已經獲得很好的回答,但這是「 $ @」 $ @「 $ *」和$ *之間的比較
測試腳本的內容:
# cat ./test.sh #!/usr/bin/env bash echo "=================================" echo "Quoted DOLLAR-AT" for ARG in "$@"; do echo $ARG done echo "=================================" echo "NOT Quoted DOLLAR-AT" for ARG in $@; do echo $ARG done echo "=================================" echo "Quoted DOLLAR-STAR" for ARG in "$*"; do echo $ARG done echo "=================================" echo "NOT Quoted DOLLAR-STAR" for ARG in $*; do echo $ARG done echo "================================="
如今,使用各類參數運行測試腳本:
# ./test.sh "arg with space one" "arg2" arg3 ================================= Quoted DOLLAR-AT arg with space one arg2 arg3 ================================= NOT Quoted DOLLAR-AT arg with space one arg2 arg3 ================================= Quoted DOLLAR-STAR arg with space one arg2 arg3 ================================= NOT Quoted DOLLAR-STAR arg with space one arg2 arg3 =================================