Re: shift command! (205 Views)
Reply
Advisor
Rinky
Posts: 27
Registered: ‎05-03-2007
Message 1 of 9 (206 Views)

Shift command! Please help

Hi everybody,

I am facing problem with the shift command.

I have 3 shell scripts:
1.Master.ksh
2.Intermediate.ksh
3.Final.ksh

The Master.ksh is calling Intermediate.ksh first and then calling Final.ksh.
In intermediate.ksh, at the end shift command is used.
Final.ksh is expecting a parameter to be passed to it.
Now because of this shift command, the Final.ksh is not recognising the parameter passed to it.
I cannot change intermediate.ksh script, and Final.ksh has to take a parameter.
Please help me how I can resolve this issue.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 2 of 9 (205 Views)

Re: Shift command! Please help

If you can't change the intermediate shell then you cannot remove the shift. You did not say that you couldn't change the Master.ksh script. I would capture the parameter in question, set a variable and export it and then use the exported variable in Final.ksh.

Plan B. Whatever the last argument is that you send to Master.ksh (or whatever the last argument that Master.ksh uses when intermediate.ksh is called) should be captured in a variable and sent to intermediate.ksh twice. That way, the shift will remove one but not the final argument.
If it ain't broke, I can fix that.
Acclaimed Contributor
Dennis Handly
Posts: 25,279
Registered: ‎03-06-2006
Message 3 of 9 (205 Views)

Re: shift command!

[ Edited ]

>The Master.ksh is calling Intermediate.ksh first and then calling Final.ksh.

Master calls intermediate and then master calls final? Or intermediate calls final?

>In intermediate.ksh, at the end shift command is used.

A shift in intermediate.ksh can't change the parms in Master.ksh, my first assumption.

>I cannot change intermediate.ksh script, and Final.ksh has to take a parameter.

It would help if we had an example of Master.ksh and how the parms are moved around.

Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 4 of 9 (205 Views)

Re: Shift command! Please help

are you sourcing the different shells?
( . ./Intermediate.ksh )
then something like:
SAVARGS="$*"
. ./Intermediate.ksh
set -- "$SAVARGS"

. ./Final.ksh

Honored Contributor
Jeroen Peereboom
Posts: 2,732
Registered: ‎06-26-2003
Message 5 of 9 (205 Views)

Re: Shift command! Please help

Call the master script with the last argument duplicated: in stead of
Master a b c
do
Master a b c c

Maybe you are calling the Master script incorrectly?

HtH,

JP
Honored Contributor
Jeroen Peereboom
Posts: 2,732
Registered: ‎06-26-2003
Message 6 of 9 (205 Views)

Re: Shift command! Please help

Hmm,

a correction:
Master calls intermediate, intermediate shifts and exits, master continues and call final.
The shift in intermediate only affects the argument list passed on to intermedaiate and has no influence on calling the final script.

I just tested this in ksh on Linux.

So your problem is not caused by the intermediate script.

JP
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 7 of 9 (205 Views)

Re: Shift command! Please help

the "master" script must be "sourcing" the "intermediate" script. in that specific case, the intermediate script is not a sub-shell and therefore can shift the args.

you're going to have to modify the master to save the original arg list, and then call the "final" script w/ the saved args
Acclaimed Contributor
Dennis Handly
Posts: 25,279
Registered: ‎03-06-2006
Message 8 of 9 (205 Views)

Re: shift command!

[ Edited ]

>Laurent: then something like: SAVEARGS="$*"

The proper way to save args is:
set -A SAVEARGS -- "$@"

The proper way to reset them is:
set -- "${SAVEARGS[@]}"

Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 9 of 9 (205 Views)

Re: Shift command! Please help

You are right works better if you have blanks inside params.
SAV="$*"

set -- $SAV

for instance "A B" "C D"
with my method will become
A B C D ( 4 parameters)

and
set -- "$SAV"
will become
"A B C D" (1 parameter)
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.