arguments for command file ( long, pedantic response )

Peter Klosky peter at rlgvax.UUCP
Sat Dec 22 06:27:54 AEST 1984


>  
> 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.
> cd $1
> 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.

You do not say which shell you are using, as in sh or csh, but at any
rate the problem is that your command file is probably being
interpreted by a subprocess, and while you have managed to get the 
working directory of the subprocess changed, this does not change your
parent's current working directory.  You have similar trouble if
you try to change an environment variable in a subshell.  This is a 
feature, btw, not a bug.

A solution is to have the command file read by the current shell instead
of in a subshell.  With the Bourne shell (sh) you can "dot" the file
as in  

. cd_command_file

with the Joy shell (csh), you can alias "cd" to source a command file 
which will perform your stuff then chdir to the argument.  Here is a 
"cd" alias and associated command file which will perform just like cd,
and also maintain a "trail of bread crumbs" which you can follow back
via the "." alias also included.  Also you can switch between two 
directories via the "sw" alias.

PART A: ( aliases for .login )

set l='(' m=')'
alias cd 'set arg=\!*;source ~/.pushd;unset arg;set prompt="${l}\\!${m} ${cwd}: "'
alias sw 'pushd >& /dev/null;set prompt="${l}\\!${m} ${cwd}: "'
alias .  'popd > /dev/null ;set prompt="${l}\\!${m} ${cwd}: "'
set prompt="${l}\!${m} ${cwd}: "

PART B: ( command file sourced by cd aliase; ~/.pushd )

if ( "$arg" == "" ) then
  pushd ~ >/dev/null
else
  pushd $arg >/dev/null
endif



More information about the Comp.unix.wizards mailing list