diff options
Diffstat (limited to 'bash')
| -rwxr-xr-x | bash/.bashrc | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/bash/.bashrc b/bash/.bashrc index bfba031..3e9a0e2 100755 --- a/bash/.bashrc +++ b/bash/.bashrc | |||
| @@ -43,6 +43,50 @@ exists() { | |||
| 43 | hash $@ 2> /dev/null | 43 | hash $@ 2> /dev/null |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | is_function() { | ||
| 47 | function=$(type $@ 2> /dev/null | awk '/is a function/ { print "1" }') | ||
| 48 | if [ -n "$function" ]; then | ||
| 49 | return 0 | ||
| 50 | else | ||
| 51 | return 1 | ||
| 52 | fi | ||
| 53 | } | ||
| 54 | |||
| 55 | is_alias() { | ||
| 56 | alias=$(type $1 2> /dev/null | awk '/is aliased to/ { print "1" }') | ||
| 57 | if [ -n "$alias" ]; then | ||
| 58 | return 0 | ||
| 59 | else | ||
| 60 | return 1 | ||
| 61 | fi | ||
| 62 | } | ||
| 63 | |||
| 64 | if exists sudo; then | ||
| 65 | sudo() { | ||
| 66 | # Allow functions and aliasses to be executed with sudo | ||
| 67 | if is_function $1 2> /dev/null; then | ||
| 68 | tmpfile="/tmp/$(date +%s).sh" | ||
| 69 | |||
| 70 | echo "#!$usr/bin/env bash" > "$tmpfile" | ||
| 71 | type $1 | grep -v "is a function" >> "$tmpfile" | ||
| 72 | echo "$@" >> "$tmpfile" | ||
| 73 | |||
| 74 | chmod +x "$tmpfile" | ||
| 75 | |||
| 76 | $usr/bin/sudo "$tmpfile" | ||
| 77 | |||
| 78 | rm "$tmpfile" | ||
| 79 | elif is_alias $1 2> /dev/null; then | ||
| 80 | alias=$(type $1 2> /dev/null | | ||
| 81 | sed -n "s/^$1\ is\ aliased\ to\ \`\(.*\)'$/\1/p") | ||
| 82 | |||
| 83 | $usr/bin/sudo "$alias" | ||
| 84 | else | ||
| 85 | $usr/bin/sudo "$@" | ||
| 86 | fi | ||
| 87 | } | ||
| 88 | fi | ||
| 89 | |||
| 46 | if exists complete && exists sudo; then | 90 | if exists complete && exists sudo; then |
| 47 | complete -cf sudo | 91 | complete -cf sudo |
| 48 | fi | 92 | fi |
