From 390f41ebc5751db4fd1bc2af279eb58aa8ac5475 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 7 Aug 2015 00:11:35 +0200 Subject: Added execute functions and aliasses with sudo to bashrc --- bash/.bashrc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'bash') diff --git a/bash/.bashrc b/bash/.bashrc index bfba031..3e9a0e2 100755 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -43,6 +43,50 @@ exists() { hash $@ 2> /dev/null } +is_function() { + function=$(type $@ 2> /dev/null | awk '/is a function/ { print "1" }') + if [ -n "$function" ]; then + return 0 + else + return 1 + fi +} + +is_alias() { + alias=$(type $1 2> /dev/null | awk '/is aliased to/ { print "1" }') + if [ -n "$alias" ]; then + return 0 + else + return 1 + fi +} + +if exists sudo; then + sudo() { + # Allow functions and aliasses to be executed with sudo + if is_function $1 2> /dev/null; then + tmpfile="/tmp/$(date +%s).sh" + + echo "#!$usr/bin/env bash" > "$tmpfile" + type $1 | grep -v "is a function" >> "$tmpfile" + echo "$@" >> "$tmpfile" + + chmod +x "$tmpfile" + + $usr/bin/sudo "$tmpfile" + + rm "$tmpfile" + elif is_alias $1 2> /dev/null; then + alias=$(type $1 2> /dev/null | + sed -n "s/^$1\ is\ aliased\ to\ \`\(.*\)'$/\1/p") + + $usr/bin/sudo "$alias" + else + $usr/bin/sudo "$@" + fi + } +fi + if exists complete && exists sudo; then complete -cf sudo fi -- cgit v1.2.3