add proper error handling for all final exec calls
[hband-tools.git] / user-tools / nopath
blobdc34c05823be884324711956f93c5745ddfd1629
1 #!/bin/bash
3 true <<'EOF'
4 =pod
6 =head1 NAME
8 nopath - Run command by disregarding some PATH directories
10 =head1 SYNOPSIS
12 nopath [-r|--recursive] I<DIRLIST> I<COMMAND> [I<ARGS>]
14 =head1 DESCRIPTION
16 Run I<COMMAND> by looking up directories in C<PATH> environment variable
17 as usual, except ignore those directories which are given in colon-delimited list, I<DIRLIST>.
19 Useful in situations like you have a wrapper command X which runs the "real" command X and
20 don't want to burn in the wrapped command's full path, but keep relying on PATH-lookup mechanisms.
22 =cut
24 EOF
27 set -e
28 set -o pipefail
29 set -u
31 . /usr/lib/tool/bash-utils
34 opt_recursive=''
36 while [ $# != 0 ]
38 case "$1" in
39 --help)
40 pod2text "$0"
41 exit 0;;
42 -r|--recursive)
43 opt_recursive=1
45 --)
46 shift
47 break;;
48 -*)
49 errx -1 "unknown option: $1";;
51 break;;
52 esac
53 shift
54 done
56 IFS=':'
57 declare -a nopaths=($1)
58 declare -a sys_paths=($PATH)
59 IFS=$' \t\n'
60 shift
62 sys_path=$PATH
64 declare -a paths=()
65 for dir in ${sys_paths[@]}
67 if ! in_list "$dir" "${nopaths[@]}" && ! in_list "$dir/" "${nopaths[@]}" && ! in_list "${dir//%+(\/)}" "${nopaths[@]}"
68 then
69 paths+=("$dir")
71 done
73 PATH=`bash_join : "${paths[@]}"`
75 if [ $opt_recursive ]
76 then
77 exec "$@"
78 else
79 exec env PATH="$sys_path" "$@"