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
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:
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.
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.
Do you want the user to exit your script and be in the correctory directory?
Then source your script. ie., (note the leading dot)
. /path/to/script.sh
If you are invoking shells ie.,
the first line is
#!usr/bin/sh
then this idea will not work because everytime the user calls the script he execs another shell. Get rid of it. In a sourced script you also should not call exit at the end of the script.
If you read the title you will see that the question relates to a bourne shell SCRIPT. I suggested an alias in the users' environment, not in the script. I have no idea what other users use as login shells. Why would I suggest set an alias in a script to begin with, especially when, even if the script was ksh or csh, it would do what he wants???