If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > changing directory from within a script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-13-04, 19:11
varun_b varun_b is offline
Registered User
 
Join Date: May 2004
Posts: 11
changing directory from within a script

Hi,
i am trying to "cd" in my shell script but as soon as the script finishes execution the current directory becomes the old directory from which the script was run.

How can i make the change permanent.

before script execution PWD is /export/home/vbhargav

#!/bin/sh

TOCHANGE=/data/bmw/welcome/

cd TOCHANGE

echo "${PWD}" <--- this prints /data/bmw/welcome/


exit 0

After script execution PWD becomes /export/home/vbhargav again

Please help
Reply With Quote
  #2 (permalink)  
Old 09-14-04, 02:46
saurav101 saurav101 is offline
Registered User
 
Join Date: Oct 2003
Posts: 19
Varun
By default the shell runs your script in a shell which is different from the shell you logged in . So when the script exits , you come back to your own shell ,hence the old working directory .
To make the change permanent you need to run your script in the same shell as you have logged in . To do so precede your invocation script with a "." (dot) , to specify the same shell . This will change to the directory you want .
e.g $ . <myscript.sh> instead of $<myscript.sh>
Let me know if that works
Thanks
Saurav
Reply With Quote
  #3 (permalink)  
Old 09-14-04, 18:59
varun_b varun_b is offline
Registered User
 
Join Date: May 2004
Posts: 11
Still not working

I tried ./myscript.sh but the result is still the same.
Reply With Quote
  #4 (permalink)  
Old 09-15-04, 02:44
saurav101 saurav101 is offline
Registered User
 
Join Date: Oct 2003
Posts: 19
Varun
Thanks for letting me know . Guess I forgot to say that explicitly . There should be a space between the dot and the script name e.g
$. myscript.sh . This will tell the Shell to run the script under the same shell .

Let me know if this works
Thanks
Saurav
Reply With Quote
  #5 (permalink)  
Old 09-15-04, 04:47
ducasio ducasio is offline
Registered User
 
Join Date: Aug 2004
Location: Rome, Italy
Posts: 81
you must use the $ to use the value of a variable in a shell; so:

cd $TOCHANGE
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On