Sunday, 15 August 2010

How to pass an array argument to the Bash script -


It is amazing to me that I can not find an answer for 1 hour after this. I would like to pass my script in such a way:

  test.sh Argument 1 array argument 2   

I do not want to put it in any other bash Scripts like the following: "$ {array} ["] "in" $ {array = (abc) for array = (abc)) test.sh logic 1 $ i logic 2 < Bash arrays are not "first class values" - you can not give them the same thing as a "thing" .

Assuming test.sh is a Bash script, I do bir / bash arg1 = $ 1; Shift array = ("$ @") last_idx = $ ($ {# array [@]} - 1)) arg2 = $ Array [$ last_idx]} array set [$ last_idx] echo "arg1 = $ arg1" echo "Arg2 = $ arg2" echo "array includes:" printf "% s \ n" "$ {array { ]} "

and invite it like

  test.sh Argument 1" $ {array {@}} "argument 2  < / Pre>  

No comments:

Post a Comment