Alexander Afanasyev | 24ed83a | 2015-03-30 11:06:50 -0700 | [diff] [blame] | 1 | has() { |
Davide Pesavento | 6a81b77 | 2017-08-20 18:43:46 -0400 | [diff] [blame] | 2 | local saved_xtrace |
| 3 | [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x |
| 4 | set +x |
| 5 | |
Alexander Afanasyev | 24ed83a | 2015-03-30 11:06:50 -0700 | [diff] [blame] | 6 | local p=$1 |
| 7 | shift |
Davide Pesavento | 6a81b77 | 2017-08-20 18:43:46 -0400 | [diff] [blame] | 8 | local i ret=1 |
| 9 | for i in "$@"; do |
| 10 | if [[ "${i}" == "${p}" ]]; then |
| 11 | ret=0 |
| 12 | break |
| 13 | fi |
Alexander Afanasyev | 24ed83a | 2015-03-30 11:06:50 -0700 | [diff] [blame] | 14 | done |
Davide Pesavento | 6a81b77 | 2017-08-20 18:43:46 -0400 | [diff] [blame] | 15 | |
| 16 | set ${saved_xtrace} |
| 17 | return ${ret} |
Alexander Afanasyev | 24ed83a | 2015-03-30 11:06:50 -0700 | [diff] [blame] | 18 | } |
Davide Pesavento | f6be8e3 | 2020-04-30 16:06:03 -0400 | [diff] [blame^] | 19 | export -f has |
Davide Pesavento | 0b0eddd | 2019-03-19 14:42:15 -0400 | [diff] [blame] | 20 | |
| 21 | sudo_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 Pesavento | f6be8e3 | 2020-04-30 16:06:03 -0400 | [diff] [blame^] | 39 | export -f sudo_preserve_env |