The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 10:30 PM
Bourne: How to invoke an alias from within a shell script techshots Shell Programming and Scripting 2 06-04-2006 12:38 AM
Bourne Shell Script dmhonor914 UNIX for Dummies Questions & Answers 2 12-10-2003 12:25 PM
bourne shell script psrinivas Shell Programming and Scripting 2 12-06-2001 03:38 PM
Bourne shell script need help please ? dezithug UNIX for Advanced & Expert Users 7 10-24-2001 01:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-22-2006
fawqati fawqati is offline
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
  #2 (permalink)  
Old 05-22-2006
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
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.
  #3 (permalink)  
Old 05-22-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,402
See cd in shell script
and http://www.unix.com/unix-advanced-expert-users/18725-using-alias.html#post71611
  #4 (permalink)  
Old 05-23-2006
fawqati fawqati is offline
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
  #5 (permalink)  
Old 05-23-2006
System Shock's Avatar
System Shock System Shock is offline Forum Advisor  
Registered User
  
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 521
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.
  #6 (permalink)  
Old 05-23-2006
fawqati fawqati is offline
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.
  #7 (permalink)  
Old 05-23-2006
System Shock's Avatar
System Shock System Shock is offline Forum Advisor  
Registered User
  
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 521
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0