Solved

Want cd in sh script to change parent working directory

Posted on 1997-12-31
6
233 Views
Last Modified: 2013-12-26
I have written a /bin/sh program to look up and verify write access to a user's home directory (not the caller's HOME).  I would like this procedure to cd to the new directory and make the parent process's working directory the new directory.  The script is

...
DIR=$( finger $1 | grep | Directory | awk {print $2})
... # verify permissions, existance etc.
cd $DIR

This proc works and changes the working directory within the
local script, but when the script terminates, the parent process (caller) is still in its original directory.

There are two parts to this question:
a.)  Can I make the parent's wd the same as the new wd?
b.)  Can I keep this script alive and permit the user to do interactive work within this script?

It would almost be a better option to make the script do (b) since when the user finishes messing around in the other directory the exiting script would automatically take them back where they started.  (My expertise is Vax/VMS DCL, OS is FreeBSD V2.2.2)
0
Comment
Question by:avei
  • 3
  • 2
6 Comments
 

Accepted Solution

by:
emieng earned 50 total points
Comment Utility
a) no
b) no

All is not lost though. The simplest workaround would be to not execute the script in its own process, but the current shell process, via the '.' dot command. If this is unsatisfactory for some reason, another alterative would be to have your script return the directory name that you want the calling shell to cd
to, which it does explicitly.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
b.) how about ending the script with
exec /bin/sh

0
 

Expert Comment

by:emieng
Comment Utility
Ozo's suggestion would work, but it's pretty hoky on two accounts. One is that the user would have to exit out of two interactive shells when they're all done, one more than expected. Another is that changes to the first shell won't show up in the second shell because it's not the first shell, which is what the user thinks it is.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Expert Comment

by:ozo
Comment Utility
Agreed.  (But it sounds like what avei was asking for in b.)
Or perhaps you could clarify what you wanted?  Maybe more like:

#!/bin/sh
cd $DIR
echo do interactive work within $DIR
/bin/sh
echo exiting back to where you started


or, as emieng first suggested
. script.sh
or
cd `script.sh`
0
 

Expert Comment

by:emieng
Comment Utility
Of the two suggestions I put out, I now realize that b is really the only practical one. The problem with a is that the user must be running the same shell that the script is written in, which in general is unlikely. The benefit that b provides is that it decouples the user's shell from the script's shell.
0
 

Author Comment

by:avei
Comment Utility
I had already arrived at the conclusion that
exec /bin/sh was the only mechanism which would closely approximate what I had in mind, (despite the hokeyness of double exits),i.e. move the user to another user's directory which the system:
   verified the user's existance which the parser does
   verified the directory exists which the test ... command does
   verified the requestor had write permission to the requested directory and let the user take over from there.

Bear in mind that my users are very computer illiterate and are very intimidated.  This is why I like VMS's ability to do a global "set default {directory} " from within a script and have it stick in the parent shell.

Passing the parameter back to the previous shell for the user to manually change will not work very well.  Remember these are users who think the word "backslash" refers to the next guy on the line trying to get ahead.  So...what I really need to consider is writing a shell for the really clueless, sigh.

Thanks for your help guys.

PS. When I tried to respond to this question using the web address provided in the email, I got a SERVER Error.  Where do I direct this diagnx?

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This video discusses moving either the default database or any database to a new volume.

728 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

9 Experts available now in Live!

Get 1:1 Help Now