[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Portable shell scripts



csj <csj@zapo.net> writes:

> I tend to write scripts which are tcsh-compatible.  So
> "#!/bin/tcsh".  But its somewhat a waste of effort to write one
> set of scripts for bash and another for tcsh.  My main problem is
> handling the variables.  Is there a shell-portable way to specify
> variables?

...to what end?  I'm confused here: if you set variables inside the
script, they won't be visible to the parent of the script, but they
generally will be visible to children.

  #!/bin/tcsh
  setenv TESTVAR hi
  /bin/sh -c 'echo $TESTVAR'

  #!/bin/sh
  TESTVAR=hi
  export TESTVAR
  /bin/tcsh -c 'echo $TESTVAR'

will both print "hi" because that's the string being passed through
the environment; it doesn't care what shells are (or aren't) involved.

> I write mostly convenience scripts that are generally less than a
> console screen in length.

So do I, but that doesn't stop me from wanting to do

  make 2>/dev/null

occasionally.  :-)  Using a Bourne-like interactive shell is also
useful, since I wind up typing a lot of one-liners that might include
shell loops.  (I personally use zsh, but I think bash has about the
same feature set these days; at least, it has programmable completion,
which is the reason I originally switched to zsh.)

-- 
David Maze         dmaze@debian.org      http://people.debian.org/~dmaze/
"Theoretical politics is interesting.  Politicking should be illegal."
	-- Abra Mitchell



Reply to: