0

I am having a parent script and a child script,

Parent script:

echo 'Parent script 1st arg '$1
echo 'Parent script 2nd arg '$2
PARAMS=$*
ksh child_script.ksh $PARAMS

Child script:

echo 'Child script 1st arg '$1
echo 'Child script 2nd arg '$2

I am executing the parent script by passing two argument as below

ksh parent_script.ksh ABC 'DEF XYZ'

I am getting the output as,

Parent script 1st arg ABC
Parent script 2nd arg DEF XYZ
Child script 1st arg ABC
Child script 2nd arg DEF

I want to pass DEF XYZ as a single argument which is why I enclosed in single quotes and passed the same. It is considered as a single argument by the parent script. Later, when it is assigned using $* the grouping of words is not preserved. Rather it is getting passed as two different argument. Please advice why the arguments are not passed as it is and is there any way to correct it?

Expected output,

Parent script 1st arg ABC
Parent script 2nd arg DEF XYZ
Child script 1st arg ABC
Child script 2nd arg DEF XYZ
Dev Anand
  • 314
  • 1
  • 14

1 Answers1

0

For ksh, you could do something like:

#!/bin/ksh
echo "1:$1"
echo "2:$2"
if [[ $(basename $0) == 'tst' ]]
then
    # bash: P="${*@K}"
    P="$1"
    shift
    while [[ $# -gt 0 ]]
    do
        P="\"$P\" \"$1\""
        shift
    done

    echo "P:$P"
    eval ./tst2 $P
fi

link this to tst2 as well (ln tst tst2).

Note, I haven't used ksh in about 20 years so there may be a quicker way to create P (as there is in bash).

$ ./tst a 'b c d'
1:a
2:b c d
P:"a" "b c d"
1:a
2:b c d

For bash you could just do:

#!/bin/bash
echo "1:$1"
echo "2:$2"
if [[ $(basename $0) == 'tst' ]]
then
    P="${*@K}"
    echo "P:$P"
    eval ./tst2 $P
fi
Wayne Vosberg
  • 1,023
  • 5
  • 7