arguments for a command file

Rob Warnock rpw3 at redwood.UUCP
Sat Dec 22 13:03:42 AEST 1984


[ Wizards, no flames please... net.unix IS for novice questions, occasionally.]
[[ ...besides, we can't put EVERYTHING in net.announce.newusers ;-} ]]

+---------------
| I am facing a problem writing a command file. I would like this file
| to be run with an argument supplied. The command it executes is cd.
| the argument is the pathname it should default to...
| I am attempting to pass the argument by -- cd $1
| Is this the right way of doing it?
+---------------

Yes, this is the right way, generally speaking, to pass an argument to
a shell script. If you want it to default to some value, you can do use
the "${1-foobar}" construct, which will cause the argument to default
to "foobar" if you don't give it one.

+---------------
| I print the pwd in the command file and it prints the expected one.
| On using the command pwd outside the command file I find that the
| change in the directory has not been made.
| Why does it do this?
+---------------

The "current directory" is a property of each process, not of your
interaction with the terminal (sometimes called your "job"), although
new processes inherit a "current directory" from their parent process
when created ("forked").

When you execute a command file (or script), your current shell creates
a new process for the copy of the shell which is interpreting the command
file. When you change the "current directory" in your script, it ONLY
affects commands executed INSIDE the script from that point on. Your
login shell was never affected, since it never saw the "cd" command.

In fact, for this reason, the shell must execute the "cd" command
itself, without running a program. For if "cd" were a program, a
new process would be created for it to run in (as is done for ANY
program), the "current directory" would be changed FOR THAT NEW PROCESS
ONLY (i.e., the "cd" program), and your login shell would never see it.

There ARE ways to use scripts to help move your current directory around,
but it gets a little messy. You must cause your current shell to actually
read the script itself, and NOT invoke a sub-shell to read it. This can be
done if you are using the Bourne Shell (a "$" prompt) with the "." command
(yep, just a dot!). If you put the following in a file named (say) "cdnews":

	olddir=`pwd`
	cd $HOME/news
	echo 'Now in your "news" directory from' $olddir

you can type ". cdnews" (don't forget the space after the dot), and this
will do the sort of thing you wanted. (Depending on your shell, you may have
to type ". $HOME/cdnews" if "cdnews" is in your home area and you aren't.)
(Notice that you can "cd $olddir" to get back.)

If you are using the Berkeley C-shell "csh" (usually a "%" prompt), you
can use the "source cdnews" command, instead of the dot (it does the same
thing), but it's a lot easier to define some aliases to help you. Type
the command

	alias cdnews 'source $HOME/cdnews'

and from then on you can just say "cdnews", which is what you were trying
to do originally. If you do this a lot, it is good to put such aliases in
your ".cshrc" or ".login" files. Find a local friend or wizard to help.


Rob Warnock
Systems Architecture Consultant

UUCP:	{ihnp4,ucbvax!dual}!fortune!redwood!rpw3
DDD:	(415)572-2607
USPS:	510 Trinidad Ln, Foster City, CA  94404



More information about the Comp.unix mailing list