Re: [bash] Verify substring in shell var
From: Chris F.A. Johnson (c.f.a.johnson_at_rogers.com)
Date: 04/30/03
- Next message: Chris F.A. Johnson: "Re: Shell scripting help required..."
- Previous message: Laurent Vogel: "Re: [bash] Verify substring in shell var"
- In reply to: pt: "[bash] Verify substring in shell var"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: 30 Apr 2003 06:38:46 GMT
On Wed, 30 Apr 2003 at 04:23 GMT, pt wrote:
> I'm trying to verify that a shell variable (PATH) contains a directory
> and, if not, add it to PATH. I've seen:
>
> if ! echo $PATH | /bin/grep -q "/fred" ; then
> PATH=$PATH:/fred
> fi
>
> but it occurs to me I might be able to do something like this:
>
> if [[ $PATH != */fred* ]]; then
> PATH=$PATH:/fred
> fi
What if $PATH includes "/frederica"?
> Is there any argument for either method (or any six other ways to do
> the same thing)? Any reason to do it entirely "within" the shell vs.
> via external executables (like "grep")?
In any Bourne-type shell:
case :$PATH: in
*:/fred:*) ;; ## already in $PATH
*) PATH=$PATH:/fred ;;
esac
It is always faster to use an internal shell command than to call
an external command. However, unless the command is called many
times (or may commands are called), the saving is usually not
significant. Then again, you may find the code useful in another
situation, such as in a loop, so coding in the most efficient
manner will yield better results.
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
- Next message: Chris F.A. Johnson: "Re: Shell scripting help required..."
- Previous message: Laurent Vogel: "Re: [bash] Verify substring in shell var"
- In reply to: pt: "[bash] Verify substring in shell var"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|