[X-Unix] A little shell scripting help
David Ledger
dledger at ivdcs.demon.co.uk
Tue Oct 4 22:56:25 PDT 2005
>From: Larry Helms <lhelms at sonic.net>
>Now... I shall pipe in about the "if (...)" issue originally raised...
>
>I shall EXCLUDE the csh or tcsh shell for the reasons mentioned in
>http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
>
>
>Depending on the shell the (...) construct usually means execute contents
>INLINE. E.g.
>
>( ls -1; head -1 )
(...) means run in a sub-shell, not in-process (which is what I
presume you mean by INLINE). All non-builtin commands run in a
sub-process unless 'exec'd.
>Each time Unix shells hit a command they really fork off another process to
>perform the task - having used other OSes... I won't even go into that issue
>- anyway...
This is not a problem. Some other OSes have process models that make
creating a new process very expensive, so forking would be a bad way
to run commands. Unix processes are cheap to start as part of the
basic design concept. Forks have been getting cheaper over the years.
There's now very little done by a fork/exec that doesn't have to be
done anyway no matter how you load a program into memory.
> A way to force the routine NOT to do this... Is to enclose the
>commands in ( ) sets.
You cannot force the shell to run commands in-process except by using
'exec'. Then there's no way back after the first command. I have used
an OS where every command was, in effect, 'exec'd. Every command had
to be told what program to load when it terminated. A nightmare. If a
command fails or loops, you have to reboot.
>From: Doug McNutt <douglist at macnauchtan.com>
>At 02:35 -0700 10/4/05, Philip J Robar wrote:
>>'[' has been a built-in in all modern shells that use it for years.
>>The '[' in /bin is just there for compatibility.
>
>Most interesting. Why then is the space still required?
The shell still has to parse the line, however the command is
implemented. If '[' were included as a special case all sorts of
possible valid command lines would not parse correctly. We don't
often do it because of the shell [...] wildcard, but filenames can
begin with '['.
David
--
David Ledger - Freelance Unix Sysadmin in the UK.
Chair of HPUX SysAdmin SIG of hpUG technical user group (www.hpug.org.uk)
david.ledger at ivdcs.co.uk
www.ivdcs.co.uk
More information about the X-Unix
mailing list