Joao Pereira | 08f0701 | 2015-07-06 18:13:29 -0400 | [diff] [blame] | 1 | has() { |
Davide Pesavento | e0edc4c | 2016-09-13 18:29:52 +0000 | [diff] [blame] | 2 | local saved_xtrace |
| 3 | [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x |
| 4 | set +x |
| 5 | |
Joao Pereira | 08f0701 | 2015-07-06 18:13:29 -0400 | [diff] [blame] | 6 | local p=$1 |
| 7 | shift |
Davide Pesavento | e0edc4c | 2016-09-13 18:29:52 +0000 | [diff] [blame] | 8 | local i ret=1 |
| 9 | for i in "$@"; do |
| 10 | if [[ "${i}" == "${p}" ]]; then |
| 11 | ret=0 |
| 12 | break |
| 13 | fi |
Joao Pereira | 08f0701 | 2015-07-06 18:13:29 -0400 | [diff] [blame] | 14 | done |
Davide Pesavento | e0edc4c | 2016-09-13 18:29:52 +0000 | [diff] [blame] | 15 | |
| 16 | set ${saved_xtrace} |
| 17 | return ${ret} |
Joao Pereira | 08f0701 | 2015-07-06 18:13:29 -0400 | [diff] [blame] | 18 | } |
Davide Pesavento | dda258a | 2018-09-06 02:43:10 -0400 | [diff] [blame] | 19 | |
| 20 | sudo_preserve_env() { |
| 21 | local saved_xtrace |
| 22 | [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x |
| 23 | set +x |
| 24 | |
| 25 | local vars=() |
| 26 | while [[ $# -gt 0 ]]; do |
| 27 | local arg=$1 |
| 28 | shift |
| 29 | case ${arg} in |
| 30 | --) break ;; |
| 31 | *) vars+=("${arg}=${!arg}") ;; |
| 32 | esac |
| 33 | done |
| 34 | |
| 35 | set ${saved_xtrace} |
| 36 | sudo env "${vars[@]}" "$@" |
| 37 | } |