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

Re: Bash: testing a variable



I am sorry I found myself in the wrong thread ... so discard

Am 09.07.21 um 22:25 schrieb Greg Wooledge:
On Fri, Jul 09, 2021 at 01:54:19PM -0600, Charles Curley wrote:
I'd like to check two things:

* Whether a given path is not already in the $PATH variable

* Whether the given path already exists

If both are true, I'd like to add the given path to the $PATH variable.
add_path_maybe() {
  local p i

  if [[ $1 != /* ]]; then
    echo "refusing to consider a directory that doesn't begin with /" >&2
    return 1
  fi

  if [[ ! -d $1 ]]; then
    echo "directory '$1' does not exist" >&2
    return 1
  fi

  IFS=: read -ra p <<< "$PATH"
  for i in "${p[@]}"; do
    if [[ $i = "$1" ]]; then
      return 0
    fi
  done

  PATH=$PATH:$1
}

-- 
=============================================================================
Joerg Kampmann, Dr. Dipl.-Phys - IBK-Consult for Climate Physics - non-profit - 
D-31228 Peine +49-177-276-3140
www.ibk-consult.de - www.kampmannpeine.org
www.xing.com/hp/Joerg_Kampmann
www.xing.com/net/mathe
www.researchgate.net/profile/Joerg_Kampmann - https://independent.academia.edu/J%C3%B6rgKampmann
===============================================================================
This e-mail may contain confidential and/or legally protected information.
If you are not the intended recipient (or have received this e-mail in
error) please notify the sender immediately and delete this e-mail. Any
unauthorized copying, disclosure use or distribution of the material in
this e-mail is strictly forbidden.
Diese E-Mail enthält vertrauliche und/oder rechtlich geschuetzte Informationen.
Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtuemlich
erhalten haben, informieren Sie bitte sofort den Absender und loeschen
Sie diese Mail
===============================================================================

Reply to: