Storing ksh input array to variable and passing to another script

13,286

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!

Share:
13,286
Mike
Author by

Mike

Updated on June 05, 2022

Comments

  • Mike
    Mike about 2 years

    I have to modify 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?

  • Katerberg
    Katerberg over 13 years
    Relevant to my interests. Thanks for the help though!
  • Chris Aldrich
    Chris Aldrich almost 12 years
    See unix.stackexchange.com/questions/41357/… for the reasoning why.