blob: 8077a747b19646b026801a499b0fe5817c210870 [file] [log] [blame]
Alexander Afanasyevc7c99002015-10-09 17:27:30 -07001has() {
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -08002 local saved_xtrace
3 [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x
4 set +x
5
Alexander Afanasyevc7c99002015-10-09 17:27:30 -07006 local p=$1
7 shift
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -08008 local i ret=1
9 for i in "$@"; do
10 if [[ "${i}" == "${p}" ]]; then
11 ret=0
12 break
13 fi
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070014 done
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -080015
16 set ${saved_xtrace}
17 return ${ret}
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070018}
Davide Pesaventofee7ee32020-04-15 02:45:07 -040019export -f has
Davide Pesavento4a315b32018-11-24 14:32:19 -050020
21sudo_preserve_env() {
22 local saved_xtrace
23 [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x
24 set +x
25
26 local vars=()
27 while [[ $# -gt 0 ]]; do
28 local arg=$1
29 shift
30 case ${arg} in
31 --) break ;;
32 *) vars+=("${arg}=${!arg}") ;;
33 esac
34 done
35
36 set ${saved_xtrace}
37 sudo env "${vars[@]}" "$@"
38}
Davide Pesaventofee7ee32020-04-15 02:45:07 -040039export -f sudo_preserve_env