?
Solved

Want cd in sh script to change parent working directory

Posted on 1997-12-31
6
Medium Priority
?
243 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

764 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