Solved

Executing shell Command in the parent shell

Posted on 2004-08-12
6
874 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 101
ADO Memory leak with DELPHI 2007 37 186
matchUp  challenge 6 65
sumHeights2  challenge 7 107
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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