Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Want cd in sh script to change parent working directory

Posted on 1997-12-31
6
Medium Priority
?
246 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
[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
  • 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 84

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 84

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

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!

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

610 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