Link to home
Start Free TrialLog in
Avatar of avei
avei

asked on

Want cd in sh script to change parent working directory

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)
ASKER CERTIFIED SOLUTION
Avatar of emieng
emieng

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
b.) how about ending the script with
exec /bin/sh

Avatar of emieng
emieng

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.
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`
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.
Avatar of avei

ASKER

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?