开发者

Storing ksh input array to variable and passing to another script

开发者 https://www.devze.com 2023-01-09 07:19 出处:网络
I have to modi开发者_如何学JAVAfy an existing ksh script which looks at the command-line arguments using \'shift\', and so empties $@, but now want to pass the original arguments to a second script af

I have to modi开发者_如何学JAVAfy an existing ksh script which looks at the command-line arguments using 'shift', and so empties $@, but now want to pass the original arguments to a second script afterwards.

In the mainline case I can do this by coping $@ to a variable and passing that to the second script, but I can't get it to work for quoted command-line arguments.

If I have a script called 'printer' like below:

#!/bin/ksh

INPUT=$@  
echo "Printing args"
until [[ $# -eq 0 ]];do
   echo $1
   shift
done

./printer2 $INPUT

and printer2 like below:

#!/bin/ksh

echo "Printing second args"
until [[ $# -eq 0 ]];do
   echo $1
   shift
done

I would like the output of

./printer first second "third forth"

to be :

Printing args
first
second
third forth
Printing second args
first
second
third forth

I've tried various combinations of quotes around variables (both in the assignment of $INPUT and when passing it to printer2) but can't figure it out. Can anyone help?


Ok I think I've found the solution after an awful lot of trial and error.

Assigning $INPUT like this:

set -A INPUT "$@"

and then passing it like this:

./printer2 "${INPUT[@]}"

produces the output I'm after.

The whole first script is therefore:

#!/bin/ksh

set -A INPUT "$@"  
echo "Printing args"
until [[ $# -eq 0 ]];do
   echo $1
   shift
done

./printer2 "${INPUT[@]}"

and

./printer first second "third fourth"

outputs:

Printing args
first
second
third fourth
Printing second args
first
second
third fourth

If anyone wants to explain the problem with the other things I tried, please do, as I'm still interested!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号