Bash script เพื่อรับและส่งซ้ำพารามิเตอร์ที่ยกมา


98

ฉันกำลังพยายามรับพารามิเตอร์ที่ยกมาของสคริปต์ทุบตีเพื่อให้ได้รับสคริปต์ที่ซ้อนกันอย่างปลอดภัย ความคิดใด ๆ ?

test.sh

#!/bin/bash
echo $*
bash myecho.sh $*

myecho.sh

#!/bin/bash
 echo $1
 echo $2
 echo $3
 echo $4

Sample:

bash test.sh aaa bbb '"ccc ddd"'

Result:

aaa bbb "ccc ddd"
aaa
bbb
"ccc
ddd"

Wanted result

aaa bbb "ccc ddd"
aaa
bbb
ccc ddd

2
I was just about to ask that question! Good timing.
Scottie T

คำตอบ:


70
#!/bin/bash
echo $*
bash myecho.sh "$@"

Note the "$@" construct is not bash specific and should work with any POSIX shell (it does with dash at least). Note also that given the output you want, you don't need the extra level of quoting at all. I.E. just call the above script like:

./test.sh 1 2 "3 4"

5
"$@" works with any Bourne shell or Bourne shell derivative (from 1978 onwards), including Korn and Bash. Probably 95% of the time, using "$@" is correct and $* is wrong.
Jonathan Leffler

Nice! But, know somebody if there is any way to store it "as is" in a variable? Original $@ is not available inside a function (because it is overwritten by function arguments). I tried foovar="$@" and foovar=$@ + "$foovar"in the function and none worked :-/
bitifet

143

You want to use "$@" (quoted dollar at) to pass parameters to a subscript. Like so ....

ls-color.sh:

#!/bin/bash
/bin/ls --color=auto "$@"    # passes though all CLI-args to 'ls'


As to why.....

From the Bash man page:

$* -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

$@ -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).


Setting up some demo scripts ...

echo 'echo -e "\$1=$1\n\$2=$2\n\$3=$3\n\$4=$4"' > echo-params.sh
echo './echo-params.sh $*' > dollar-star.sh
echo './echo-params.sh $@' > dollar-at.sh
echo './echo-params.sh "$*"' > quoted-dollar-star.sh
echo './echo-params.sh "$@"' > quoted-dollar-at.sh
chmod +x *.sh

"$@" - quoted-dollar-at is an identity transformation for re-passing args to a subshell (~99% of the time, this is what you meant to do):

./quoted-dollar-at.sh aaa '' "'cc cc'" '"ddd ddd"'
  # $1= aaa
  # $2=            
  # $3= 'cc cc'
  # $4= "ddd ddd"

"$*" - quoted-dollar-star smashes the args into a single string (~1% of the time you actually want this behavior, eg in a conditional: if [[ -z "$*" ]]; then ...):

./quoted-dollar-star.sh aaa '' "'cc cc'" '"ddd ddd"'
  # $1= aaa  'cc cc' "ddd ddd"   
  # $2=                     
  # $3=             
  # $4=

$* / $@ - without quotes, both forms strip off one level of quotation and interpret spaces from the underlying strings but ignore quotation characters (almost always, this is a mistake):

./dollar-star.sh aaa '' "'cc cc'" '"ddd ddd"'
  # $1= aaa
  # $2= 'cc                  
  # $3= cc'
  # $4= "ddd

./dollar-at.sh aaa '' "'cc cc'" '"ddd ddd"'
  # $1= aaa
  # $2= 'cc
  # $3= cc'
  # $4= "ddd

If you want to have some fun, you can use "$@" to nest things as deep as you'd like, pushing and popping elements off the args stack if you like.

function identity() {
  "$@"
}
set -x
identity identity identity identity identity echo Hello \"World\"
# + identity identity identity identity identity echo Hello '"World"'
# + identity identity identity identity echo Hello '"World"'
# + identity identity identity echo Hello '"World"'
# + identity identity echo Hello '"World"'
# + identity echo Hello '"World"'
# + echo Hello '"World"'
# Hello "World"

1
Thanks for the explanation. Just used "$*" for grep alias.
darkless

You save my day!
xyz
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.