Re: Absolute pathnames to commands in shell scripts
From: Michael Tosch (eedmit_at_NO.eed.SPAM.ericsson.PLS.se)
Date: 01/29/04
- Next message: Michael Tosch: "Re: How to rearrange a 'grep' command in LINUX to use in MATLAB with UNIX?"
- Previous message: Jukka Salmi: "Re: how to output a control char with /bin/sh?"
- In reply to: Clyde Ingram: "Absolute pathnames to commands in shell scripts"
- Next in thread: Stephane CHAZELAS: "Re: Absolute pathnames to commands in shell scripts"
- Reply: Stephane CHAZELAS: "Re: Absolute pathnames to commands in shell scripts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Jan 2004 16:33:01 GMT
In article <7c6ed517.0401290156.4685f426@posting.google.com>, clyde.ingram@edl.uk.eds.com (Clyde Ingram) writes:
> What are readers views of shell scripts calling other programs by
> specifying absolute pathname?
>
> Some alternative choices I am aware of, to call commands such as:
>
> /bin/ls
> /opt/myapps/mybin/myprog
>
> are:
>
>
> 1. Put all directories in the PATH
>
> PATH=/bin:/usr/bin:/opt/myapps/mybin
>
> ls /a/b/c/d
> myprog -a aaa -b bbb
>
> I prefer this method. It makes for less cluttered looking code than
> if full pathname is specified, and less cryptic than seeing lots of
> variables, like $LS, used in choice 2.
>
> In Perl, running in taint-checking mode (perl -T), the PATH must be
> nailed down in this way, not inherited from the environment.
> Of course, I could still nail down the PATH, but also call programs
> using choice 2 or 3.
I prefer this, too.
In fact it is most readable and most portable.
>
>
> 2. Define all invoked commands at the top
>
> LS="/bin/ls"
> MYPROG="/opt/myapps/mybin/myprog"
>
> $LS /a/b/c/d
> $MYPROG -a aaa -b bbb
>
> I think this is widely practiced.
You are right.
There must be bad shell courses out there...
> An advantage is that you can test for the existance of executables,
> before trying to call them:
>
> [[ -x $MYPROG ]] || { print -u2 "$MYPROG: No such executable";
> exit 1; }
The existings of executable tools like ls,awk,sed can often be taken
for granted.
On the other hand, most differences are in tools behavior, arguments,
capabilities.
>
> (Admittedly, I practice this choice in Perl, for safe maintenance.
> But far less often in shell scripts, since shells have far fewer
> built-ins, and peppering code with the likes of $LS and $CAT looks
> ugly.)
After reading a book about quality, you might want to go for "safe
maintenance".
After a while you will find that your code gets more and more ugly,
and lacks quality.
>
>
> 3. Spell out the full pathname
>
> /bin/ls /a/b/c/d
> /opt/myapps/mybin/myprog -a aaa -b bbb
>
> This avoids the ugliness of variables like $LS, but having to spell
> out the (right) pathname for every command (and know which ones are
> built-ins, so have no pathname) seems plain awkward.
... so is ugly as well. In addition: inflexible.
>
>
> A separate issue is whether production scipts should inherit the PATH
> from their (possibly uncontrolled) environment, or nail it down,
> either explicitly like in choice 1, or implicitly as in:
>
> . /opt/myapps/mybin/myenv
if myenv is common to many scripts.
>
> (which presumably defines PATH for any caller).
>
If security matters, nail it down (PATH and IFS).
The normal method is to prepend it:
PATH=/bin:/usr/bin:/opt/myapps/mybin:$PATH
If you *want* the users to take influence, append it:
PATH=${PATH}:/bin:/usr/bin:/opt/myapps/mybin
-- Michael Tosch IT Specialist HP Managed Services Germany Phone +49 2407 575 313
- Next message: Michael Tosch: "Re: How to rearrange a 'grep' command in LINUX to use in MATLAB with UNIX?"
- Previous message: Jukka Salmi: "Re: how to output a control char with /bin/sh?"
- In reply to: Clyde Ingram: "Absolute pathnames to commands in shell scripts"
- Next in thread: Stephane CHAZELAS: "Re: Absolute pathnames to commands in shell scripts"
- Reply: Stephane CHAZELAS: "Re: Absolute pathnames to commands in shell scripts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|