[PLUG] Shell script problem
Sat Aug 4 00:08:37 PDT 2007
On Saturday 04 Aug 2007 12:13:08 Sushant Bhadkamkar wrote:
> Hi all,
> Yesterday I was writing a simple script which when run, would create a
> directory with today's date and then would take you to the created
> dir.Atfirst I thought it will be easy but the 'finding yourself in the
> directory after running the script' part didn't work out.The script I wrote
> is as follows:
> DIR=`date --iso`
> mkdir `date --iso`
> cd "$DIR"
> This did create a directory but I wasn't taken to it.After a bit of
> experimenting I found out that adding /bin/sh at the end (ie opening any
> subshell) does the work.
No it doesnt, at least not with the way you wanted. The shellscript is run in
child process. So after shell script exits, the current directory is back to
that of parent process i.e. from where you ran the script.
When you invoke the /bin/sh from the shell script, it starts a sub shell of
the shellscript and that being interactive, waits for user inputs. When you
exit this shell, the shellscript finishes.
> My question is why does the script work this way?Is there any other way to
> do this?
Run with the . i.e invoke the shellscript as '. ./dir.sh' and the shell script
is run in the current process. It should do what you want and you don't need
to invoke yet another shell from the script.
man bash for more details.
More information about the plug-mail