-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudo_wrapper
More file actions
64 lines (60 loc) · 1.41 KB
/
sudo_wrapper
File metadata and controls
64 lines (60 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
# above for documentation: you actually must source this file
# from "wilo108"'s comment on webupd8.org: http://disq.us/p/1hfd51
# Wrap sudo to handle aliases and functions
sudo () {
local c o t parse
# Parse sudo args
OPTIND=1
while getopts xVhlLvkKsHPSb:p:c:a:u: t; do
if [ "$t" = x ]; then
parse=true
else
o="$o -$t"
[ "$OPTARG" ] && o="$o $OPTARG"
fi
done
shift $(( OPTIND - 1 ))
# If no arguments are left, it's a simple call to sudo
if [ $# -ge 1 ]; then
c="$1";
shift;
case $(type -t "$c") in
"")
echo No such command "$c"
return 127
;;
alias)
c="$(type "$c"|sed "s/^.* to \`//;s/.$//")"
;;
function)
c=$(type "$c"|sed 1d)";\"$c\""
;;
*)
c="\"$c\""
;;
esac
if [ -n "$parse" ]; then
# Quote the rest once, so it gets processed by bash.
# Done this way so variables can get expanded.
while [ -n "$1" ]; do
c="$c \"$1\""
shift
done
else
# Otherwise, quote the arguments. The echo gets an extra
# space to prevent echo from parsing arguments like -n
# Note the lovely interactions between " and ' ;-)
while [ -n "$1" ]; do
c="$c '$(echo " $1"|sed -e "s/^ //" -e "s/'/'\"'\"'/")'"
shift
done
fi
# Run the command with verbose options
#echo Executing sudo $o -- bash -x -v -c "$c" >&2
command sudo "$o" bash -c "$c"
else
#echo sudo $o >&2
command sudo "$o"
fi
}