Results 1 to 8 of 8

Thread: Safely adding dirs in /opt to PATH variable

  1. #1
    Join Date
    Nov 2005
    Beans
    403
    Distro
    Ubuntu Development Release

    Safely adding dirs in /opt to PATH variable

    Hello all,

    I have various programs that are installed in subdirs of /opt. In order to allow them to run, I obviously need to add the appropriate dirs to PATH.

    My "solution" was to add a little script, opt.sh to /etc/profile.d/ containing the following code:
    Code:
    for d in /opt/*/bin; do
            PATH="$PATH:$d"
    done
    ... but this has at least 2 problems: first, it means that if I ever rerun the profile script while logged in, $PATH winds up containing multiple entries; and in any case I believe it's not considered very safe.

    Can anyone advise at least how to get rid of the multiple entries, and (better) on any superior solutions for packages installed in subdirs of /opt ... ?

    Thanks & best wishes,

    -- Joe

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Safely adding dirs in /opt to PATH variable

    Provided that you don't have several versions of same applications co-installed under /opt I'd try something like
    Code:
    mkdir /opt/bin
    ln -s /opt/*/bin/* /opt/bin
    PATH="$PATH:/opt/bin"
    and maybe using stow to maintain the link farm.
    Last edited by schragge; April 7th, 2013 at 07:40 PM.

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Safely adding dirs in /opt to PATH variable

    Quote Originally Posted by schragge View Post
    Provided that you don't have several versions of same applications co-installed under /opt I'd try something like
    Code:
    mkdir /opt/bin
    ln -s /opt/*/bin/* /opt/bin
    PATH="$PATH:/opt/bin"
    and maybe using stow to maintain the link farm.
    This is a really, really, bad idea. It's possible that one may have multiple versions of a toolkit installed in /opt, so for obvious reasons, there will be file name clashes.

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Safely adding dirs in /opt to PATH variable

    Quote Originally Posted by WebDrake View Post
    Hello all,

    I have various programs that are installed in subdirs of /opt. In order to allow them to run, I obviously need to add the appropriate dirs to PATH.

    My "solution" was to add a little script, opt.sh to /etc/profile.d/ containing the following code:
    Code:
    for d in /opt/*/bin; do
            PATH="$PATH:$d"
    done
    ... but this has at least 2 problems: first, it means that if I ever rerun the profile script while logged in, $PATH winds up containing multiple entries; and in any case I believe it's not considered very safe.

    Can anyone advise at least how to get rid of the multiple entries, and (better) on any superior solutions for packages installed in subdirs of /opt ... ?

    Thanks & best wishes,

    -- Joe
    Just check for the existence of the directory 'd' in PATH before attempting to augment PATH variable. Perhaps:
    Code:
    for d in /opt/*/bin; do
            echo $PATH | grep "$d" > /dev/null
            if [ $? -ne 0 ]; then
                    PATH="$PATH:$d"
            fi
    done
    I'm not an expert Bash script developer, so perhaps the above can be condensed for efficiency purposes.

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Safely adding dirs in /opt to PATH variable

    Inspired by this.

    Since /etc/profile.d/ is not only for bash, I tried to keep it POSIXly correct:
    Code:
    for d in /opt/*/bin; do
      test -d "$d" || continue
      case :$PATH: in
      *:"$d":*);;
      *) PATH=${PATH:+$PATH:}$d;;
      esac
    done
    Last edited by schragge; April 7th, 2013 at 09:20 PM.

  6. #6
    Join Date
    Nov 2005
    Beans
    403
    Distro
    Ubuntu Development Release

    Re: Safely adding dirs in /opt to PATH variable

    Thanks to all of you for the thoughts and replies

    @schragge -- forgive me, my shell scripting is not too great -- can you explain the use of case etc. in that script?

    It's not clear to me whether that script will handle the case of $d$ being at the beginning or the end of $PATH (i.e. not surrounded on both sides by : ). I also don't understand the double-semicolons ;; at the end of lines.

  7. #7
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Safely adding dirs in /opt to PATH variable

    Double-semicolons are just part of syntax for the case construct, they terminate command list for particular case branch.

    Note that the case parameter I'm matching against is not $PATH, but :$PATH:. This is to ensure that
    $d$ being at the beginning or the end of $PATH (i.e. not surrounded on both sides by : )
    also gets matched.

    The logic of the case statement is following:
    If the first case pattern is matched, i.e. $d is already in $PATH then do nothing, otherwise add $d to the end of $PATH value.
    ${PATH:+$PATH:} means substitute $PATH with the part after :+ only if $PATH is set and not empty, otherwise just do PATH=$d
    Last edited by schragge; April 10th, 2013 at 08:34 AM.

  8. #8
    Join Date
    Nov 2005
    Beans
    403
    Distro
    Ubuntu Development Release

    Re: Safely adding dirs in /opt to PATH variable

    @schragge: It's a bit late coming, but just to say a big thank you for the case-based script. For whatever reason I didn't get it to work at the time (probably a typo ) but with a fresh install of 13.10 I had another look, and it's working nicely.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •