Link to home
Start Free TrialLog in
Avatar of pari205
pari205

asked on

Executing shell Command in the parent shell

HI,
    How to execute a particular command only in parent shell.
for example:
abc.sh :    

                some commands
                cd /home
                some commands

in shell prompt
            i want to execute the cd command only to the parent shell so that change the parent shell's current diretory.if i execute it with . abc.sh it will run all the commands in the parent shell.but i its not my need.
Could u guys plz give me an idea

TIA
Pari.
Avatar of yuzh
yuzh

Sorry, bad news, you can't do it with shell script, FULL STOP ...
source abc.sh
    or
. abc.sh
Avatar of pari205

ASKER

Actually this is my basic need.

If i type jsut shell script name without any dot(.) or source,i have to change my current working diretory.Is this possible??

May be this script is sample:
         dev contains only one line : cd /home

when i type dev in the shell prompt it should change the current working directory to /home

Guide me if  i am going any wrong way

-Pari.


               
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What about the following in ksh shell:

for i in $(something returning a list of directory paths)
do
   cd ${i}
   <some list of commands>
done

From your definition of what you want, this basically does exactly that.  The for loop and the cd are execute only on the "parent" shell.  The <some list of commands> will generally be fork/exec children processes of this parent script.  These children processes will be executed within the CWD is whatever the value of $i is at the time.  The "generally" means it assumes you are executing other commands of some sort e.g. sed,nawk, grep, etc.

You could even do something like this using xargs but this doesn't meet your requirement that the cd be executed within the parent process:

myscript.sh <- This contains your list of commands - something like this:
#!/bin/ksh
cd ${1}
command1
command2
command3

<command returning list of directory paths> | xargs -i -t ./myscript.sh \{\}

You might look at some examples of xargs on the net.  xargs is crazy powerful if you know how to use it.
Now that I'm looking at my xargs example - its more complex then it needs to be - :)

My example would work just fine like this:

<command returning list of directory paths> | xargs -t ./myscript.sh