The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bourne Shell script - log for users loggin on and off noodlesoup Shell Programming and Scripting 14 09-08-2006 07:30 PM
Bourne: How to invoke an alias from within a shell script techshots Shell Programming and Scripting 2 06-03-2006 09:38 PM
Bourne Shell Script dmhonor914 UNIX for Dummies Questions & Answers 2 12-10-2003 08:25 AM
bourne shell script psrinivas Shell Programming and Scripting 2 12-06-2001 11:38 AM
Bourne shell script need help please ? dezithug UNIX for Advanced & Expert Users 7 10-24-2001 10:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-22-2006
Registered User
 

Join Date: May 2006
Posts: 10
Angry cd from a Bourne Shell Script - Please Help

Dear Bourne Shell Expert,

I am trying to change the current working directory from within a Bourne Shell script. Simply enough i thought !

As I am sure you are well aware, Inside the script i echo `pwd` and it seems ok, but the shell spawns another shell to execute this and as such, when my script finishes it returns to the directory where i executed it in the first place.

i execute the script from /opt/software and simply want to jump to a given directory.

#!/usr/bin/sh

echo " Now in `pwd` ### /opt/software
cd /opt/skill
echo " Now in `pwd` ### /opt/skill

$PROMPT @ /opt/software > pwd returns /opt/software

but i want it to be /opt/skill !!!!!!

Please accept my mmost profound gratitude for your help support and valuable time in this matter.

Best Regards,

FMA
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-22-2006
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
That's how it's supposed to work. When you execute a shell script, it spawns a sub-process. When you change directory in a subprocess, it may not modify it's parent's PWD, so when it exits, you're right where you began.

If you want to execute a script in the current context, try using a function. For example, in sh you should be able to place something like this in your .profile:
Code:
cd_logs(){
 cd /var/log
 pwd
}
Next time you log in (or source your .profile again) you can type cd_logs, and you'll end up in /var/log.
Reply With Quote
  #3 (permalink)  
Old 05-22-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
See cd in shell script
and http://www.unix.com/unix-advanced-expert-users/18725-using-alias.html#post71611
Reply With Quote
  #4 (permalink)  
Old 05-23-2006
Registered User
 

Join Date: May 2006
Posts: 10
Angry Still not what i was looking for : Please help !!!

Dear all,

Im sorry to keep going on about this but i honestly do need to make a bourne shell script change the working directory of the current shell.

Is there really no simple switch to enable this operation ???

I will add a quick example to illustrate :

#################################################
#!/usr/bin/sh

echo "1. Starting Project Script from = `pwd`" ### /home/fawqati
echo "2. Please Enter Project Name : " ### vega
project_name=readline
echo " Entering Project /des/proj/$project_name " ### /des/proj/vega
cd /des/proj/$project_name

##################################################

After this executes i am still in /home/fawqati and NOT
in /des/proj/<project_name>

This is a real problem and i would be extremely gratefull for some help.


Sincerly

FMA
Reply With Quote
  #5 (permalink)  
Old 05-23-2006
System Shock's Avatar
Registered User
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 378
While I don't understand exactly why you need a script to issue a cd command, why just not create an alias? As in

Code:
 alias 'cdv=cd /des/proj/$1'
then you'll be able to type cdv vega - or whatever project you have in /des/proj - and be taken to /des/proj/vega.
Reply With Quote
  #6 (permalink)  
Old 05-23-2006
Registered User
 

Join Date: May 2006
Posts: 10
Its because i use readline to ask the user which project he want to log into ! This is one one section of a huge modular script i am writing.
Reply With Quote
  #7 (permalink)  
Old 05-23-2006
System Shock's Avatar
Registered User
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 378
Well, don't use readline to ask the user where he wants to go, then.

I have no idea what other things your huge modular script does, but whatever it does, it'll all go away once it exits - other than output redirected to a file, deleted/created files, and such - just like the cd command.

The best I can give you is:

1.- create a user for each project, so users can su to it and be in the right directory, with the right environment, etc.

2.- Use expect. Expect has an "interact' command which gives the user control of the shell back within the script.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0