Operating System - Linux
1752749 Members
4981 Online
108789 Solutions
New Discussion юеВ

Re: Shift command! Please help

 
Rinky
Advisor

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.
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor

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.
Dennis Handly
Acclaimed Contributor

Re: shift command!

>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.

Laurent Menase
Honored Contributor

Re: Shift command! Please help

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

. ./Final.ksh

Jeroen Peereboom
Honored Contributor

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
Jeroen Peereboom
Honored Contributor

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
OldSchool
Honored Contributor

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
Dennis Handly
Acclaimed Contributor

Re: shift command!

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

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

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

Laurent Menase
Honored Contributor

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)