• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 882
  • Last Modified:

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.
0
pari205
Asked:
pari205
1 Solution
 
yuzhCommented:
Sorry, bad news, you can't do it with shell script, FULL STOP ...
0
 
ozoCommented:
source abc.sh
    or
. abc.sh
0
 
pari205Author Commented:
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.


               
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ahoffmannCommented:
alias dev="cd /home"
# or
alias dev "cd /home"

then type dev
0
 
NukfrorCommented:
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.
0
 
NukfrorCommented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now