?
Solved

Want cd in sh script to change parent working directory

Posted on 1997-12-31
6
Medium Priority
?
248 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 150 total points
ID: 1295860
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 85

Expert Comment

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

0
 

Expert Comment

by:emieng
ID: 1295862
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 85

Expert Comment

by:ozo
ID: 1295863
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
ID: 1295864
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
ID: 1295865
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

862 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