cd from a Bourne Shell Script - Please Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cd from a Bourne Shell Script - Please Help
# 1  
Old 05-22-2006
Network 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  
Old 05-22-2006
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  
Old 05-22-2006
# 4  
Old 05-23-2006
Network 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  
Old 05-23-2006
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  
Old 05-23-2006
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  
Old 05-23-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

New user needs help with bourne shell script

This is my question, below the question is the template Write and execute a Bourne shell script called homework that will From within the script, create three background processes: a) (2 points) one that saves a long listing of your hidden files to a file named hiddenlist b) (2 points) ... (4 Replies)
Discussion started by: luislozoya
4 Replies

2. Shell Programming and Scripting

Open a file from within a Bourne shell Script

I am suppose to make a program that cuts the extension off of a file. It uses that .ext to decide which program needs to be used to open the file. This only finds the name of the program from a list of programs I made. My problem is when it has the name of the program it needs to use, how can I... (3 Replies)
Discussion started by: dordrix
3 Replies

3. Shell Programming and Scripting

bourne shell script error on line containing declare...

Hi, Get the following error when running a shell script with following statement. Syntax error at line 150 : `(' is not expected 150: declare -a VPO_SEV=(Normal Warning Minor Major Critical) it runs fine using bash, so I guess the script should be using bash but is there a... (1 Reply)
Discussion started by: wilsonee
1 Replies

4. Shell Programming and Scripting

help with bourne shell script

Attempting to write a script to eventually notify me via email for when there is packetloss across the backbone. I am looking for values greater than 0% in the mtr field. #!/bin/sh target=www.google.com date +"%D"_"%T" >> /home/rich/mtr.log echo "----------------------------------------" >>... (1 Reply)
Discussion started by: closedown
1 Replies

5. Shell Programming and Scripting

Bourne Shell script - log for users loggin on and off

Hello all, I'm new to shell scripting and want to make a script that I can write to log the users logging on and off the a unix system. I have had a good look over the past few days to crack it, I think I am getting close. I want a script that runs an infinite loop to check every 5 seconds... (14 Replies)
Discussion started by: noodlesoup
14 Replies

6. Shell Programming and Scripting

Bourne: How to invoke an alias from within a shell script

Bourne: How to invoke an alias from within a shell script If I type in the alias in the command line, it runs If I insert that same alias into my shell script and run the shell script, the alias is not invoked. Help please. (2 Replies)
Discussion started by: techshots
2 Replies

7. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies

8. UNIX for Dummies Questions & Answers

Bourne Shell Script

Hello, I'm throwing this out there as a novice to the Unix world...I've been working on a project that requires me to ouput (using the echo command) a list of names in a single column format, but the problem is the input is in row format followed by a blank space...If anyone could give me a... (2 Replies)
Discussion started by: dmhonor914
2 Replies

9. Shell Programming and Scripting

bourne shell script

Hi all, Can somebody answer the following query Thanks, Srinivas A shell program that takes one or any number of file directory names as input; sorts the directories given as parameters jointly in the ascending or decending order of choice For EX : dips abc etc desc will sort the files... (2 Replies)
Discussion started by: psrinivas
2 Replies

10. UNIX for Advanced & Expert Users

Bourne shell script need help please ?

i have this assignment.. and i mad this script but there is something wrong with it.. if anyone can tell me.. watz going on... i would appreciate it.. tHnX in advance.. count=1 val=$2 op=$1 ans=0 if then if then while do ... (7 Replies)
Discussion started by: dezithug
7 Replies
Login or Register to Ask a Question