Solved

Executing shell Command in the parent shell

Posted on 2004-08-12
6
872 Views
Last Modified: 2013-12-26
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
Comment
Question by:pari205
6 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 11790764
Sorry, bad news, you can't do it with shell script, FULL STOP ...
0
 
LVL 84

Expert Comment

by:ozo
ID: 11790774
source abc.sh
    or
. abc.sh
0
 

Author Comment

by:pari205
ID: 11791064
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 11796425
alias dev="cd /home"
# or
alias dev "cd /home"

then type dev
0
 
LVL 10

Expert Comment

by:Nukfror
ID: 11816823
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
 
LVL 10

Expert Comment

by:Nukfror
ID: 11816829
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now