Solved

Executing shell Command in the parent shell

Posted on 2004-08-12
6
878 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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