VirtualBox

Ignore:
Timestamp:
Jun 27, 2022 1:41:54 PM (3 years ago)
Author:
vboxsync
Message:

tools/env.sh,RemoveDirFromPath.sh: Better RemoveDirFromPath code from Uwe, this works with both bash and kmk_ash.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/bin/RemoveDirFromPath.sh

    r95375 r95391  
    22# $Id$
    33## @file
    4 # Bash function for removing a directory from the PATH.
    5 #
    6 # This is used by tools/env.sh but cannot be a part of it because kmk_ash
    7 # freaks out when it sees the bash-specific substitutions.
     4# Shell (bash + kmk_ash) function for removing a directory from the PATH.
    85#
    96# Assumes KBUILD_HOST is set.
     
    2926RemoveDirFromPath()
    3027{
    31     MY_SEP=$1
    32     MY_DIR=$2
     28    # Parameters.
     29    local MY_SEP="$1"
     30    local MY_DIR="$2"
    3331    if test "${KBUILD_HOST}" = "win"; then
    3432        MY_DIR="$(cygpath -u "${MY_DIR}")"
    3533    fi
    3634
    37     # This should work at least back to bash 2.0 if the info page is anything to go by.
    38     PATH="${MY_SEP}${PATH}${MY_SEP}"                        # Make sure there are separators at both ends.
    39     PATH="${PATH//${MY_SEP}${MY_DIR}${MY_SEP}/${MY_SEP}}"   # Remove all (=first '/') ${MY_DIR} instance.
    40     PATH="${PATH#${MY_SEP}}"                                # Remove the leading separator we added above.
    41     PATH="${PATH%${MY_SEP}}"                                # Remove the trailing separator we added above.
     35    # Set the PATH components as script argument.
     36    local MY_OLD_IFS="${IFS}"
     37    IFS="${MY_SEP}"
     38    set -- ${PATH}
     39    IFS="${MY_OLD_IFS}"
     40
     41    # Iterate the components and rebuild the path.
     42    PATH=""
     43    local MY_SEP_PREV=""
     44    local MY_COMPONENT
     45    for MY_COMPONENT
     46    do
     47        if test "${MY_COMPONENT}" != "${MY_DIR}"; then
     48            PATH="${PATH}${MY_SEP_PREV}${MY_COMPONENT}"
     49            MY_SEP_PREV="${MY_SEP}" # Helps not eliminating empty entries.
     50        fi
     51    done
    4252}
    4353
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette