aboutsummaryrefslogtreecommitdiffstats
path: root/bash
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2015-08-07 00:11:35 +0200
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2015-08-07 00:11:35 +0200
commit390f41ebc5751db4fd1bc2af279eb58aa8ac5475 (patch)
treec17ac3705619cf68ff233b503f90251c205d2a7f /bash
parentf58c34322d7cabe8711e3eeff297b12a5bb5edce (diff)
downloaddotfiles-390f41ebc5751db4fd1bc2af279eb58aa8ac5475.tar.gz
dotfiles-390f41ebc5751db4fd1bc2af279eb58aa8ac5475.tar.bz2
dotfiles-390f41ebc5751db4fd1bc2af279eb58aa8ac5475.zip
Added execute functions and aliasses with sudo to bashrc
Diffstat (limited to 'bash')
-rwxr-xr-xbash/.bashrc44
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
46is_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
55is_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
64if 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 }
88fi
89
46if exists complete && exists sudo; then 90if exists complete && exists sudo; then
47 complete -cf sudo 91 complete -cf sudo
48fi 92fi