|
|
Subscribe / Log in / New account

Distribution of security fixes

Distribution of security fixes

Posted Aug 27, 2004 22:17 UTC (Fri) by EricBackus (guest, #2816)
In reply to: Distribution of security fixes by rwmj
Parent article: Distribution of security fixes

Note that there's a similar problem with $PATH. Doing:

    export PATH=$PATH:/usr/local/bin
could possibly give you an empty component in PATH, which we all know is a bad thing. Now, an empty PATH variable is probably less common than an empty LD_LIBRARY_PATH, but you never know...

Long ago, I wrong an "add_to_path" shell function, which only adds a path if the path exists and does the right thing if the current path is empty. It's not TOO complicated:

# Add a directory to PATH if it exists and is not already in PATH.
# PATH is $1, Directory is $2.  If PATH does not exist, don't prepend
# a ":" since that adds the current directory to it.  If optional $3
# is equal to "atstart", add the new component to the start of PATH,
# otherwise add it at end.
function add_to_path
{
    if [ -d "$2" ]; then
        p=`eval echo \"\\$$1\"`
        echo "$p" | grep -- "$2" > /dev/null
        if [ "$?" -ne 0 ]; then
            if [ -z "$p" ]; then
                eval $1=\""$2"\"
            else
                if [ "$3" = "atstart" ]; then
                    eval $1=\""$2:\$$1"\"
                else
                    eval $1=\""\$$1:$2"\"
                fi
            fi
        fi
    fi
}
With that in place, your shell script can do:
    add_to_path PATH /usr/local/bin
    add_to_path LD_LIBRARY_PATH /usr/local/lib
    add_to_path MANPATH /usr/local/man
And you ensure that things work correctly and efficiently.


to post comments

Distribution of security fixes

Posted Sep 5, 2004 16:22 UTC (Sun) by k8to (guest, #15413) [Link]

Maybe you really like your script, but it seems problemed to me. It's possible to want a dir to a path which does not currently exist. Also, it will not work on some versions of sh which do not support the 'function keyword'.

Pesonally I'd seperate add_to_path from add_to_manpath to produce the simple:

add_to_path() {
if [ "$PATH"x == x ]; then PATH=$1; fi
if [ "$2" == atstart ]; then PATH=$1:$PATH
else; PATH=$PATH:$1; fi
}
But I suppose, each to his own.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds