Re: [bash] Verify substring in shell var

From: Chris F.A. Johnson (c.f.a.johnson_at_rogers.com)
Date: 04/30/03


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


Relevant Pages

  • Sol8 and EVA hangs
    ... Secure Path 3.0D is loaded for channel failover. ... retrying command ... # Adapter hard loop ID address to use on the fibre channel bus. ...
    (SunManagers)
  • Summary: Sol8 and EVA hangs
    ... Oct 8 11:00:41 proddb SCSI transport failed: reason 'aborted': ... retrying command ... # Adapter hard loop ID address to use on the fibre channel bus. ... # before reporting I/O errors. ...
    (SunManagers)
  • Re: Using foreach loop to create radiobutton menu
    ... Your foreach loop is not the problem, the problem is how you define the -command. ... The only possible thing the interpreter can do is substitute the current value of range, which is likely the last value once your loop exited. ... One choice is double quotes. ... The double quotes means that $range gets expanded while in the loop, long before the puts command actually runs. ...
    (comp.lang.tcl)
  • Re: Issue implementing Runtime.exec() with StreamGobbler
    ... So jloader basically calls LoaderAutomationRun.java (starts the loop) ... As you can see the last line, the New Command Executer never returned. ... If your test driver is hanging, I'd guess that the test process itself is ...
    (comp.lang.java.programmer)
  • Re: What does Ctrl+C really do
    ... loop) to fgetsthe user's commands. ... As posted before there is a signal handler taking care of that. ... I interprete "windows terminal" as a Command Shell in MS-WIndows, ... Shell detects that the telnet client has been gone it terminates itself. ...
    (comp.os.vxworks)