From a0b301c9a97f70a9b42e5d754f2365820899aa70 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 30 Sep 2008 23:10:12 +0200 Subject: Added salias function from the grml zshrc --- etc/functions/salias | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 etc/functions/salias (limited to 'etc/functions') diff --git a/etc/functions/salias b/etc/functions/salias new file mode 100644 index 0000000..d8a3340 --- /dev/null +++ b/etc/functions/salias @@ -0,0 +1,37 @@ +## vim:ft=zsh +## creates an alias and precedes the command with sudo if $EUID is not zero. +## Taken from the grml zshrc - http://grml.org +local only=0 +local multi=0 +while [[ ${1} == -* ]] ; do + case ${1} in + (-o) only=1 ;; + (-a) multi=1 ;; + (--) shift ; break ;; + (-h) + printf 'usage: salias [-h|-o|-a] \n' + printf ' -h shows this help text.\n' + printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n' + printf ' be careful using this option.\n' + printf ' -o only sets an alias if a preceding sudo would be needed.\n' + return 0 + ;; + (*) printf "unkown option: '%s'\n" "${1}" ; return 1 ;; + esac + shift +done + +if (( ${#argv} > 1 )) ; then + printf 'Too many arguments %s\n' "${#argv}" + return 1 +fi + +key="${1%%\=*}" ; val="${1#*\=}" +if (( EUID == 0 )) && (( only == 0 )); then + alias -- "${key}=${val}" +elif (( EUID > 0 )) ; then + (( multi > 0 )) && val="${val// ; / ; sudo }" + alias -- "${key}=sudo ${val}" +fi + +return 0 -- cgit v1.2.3