Skip to content

simple bash function to prefix any given command with sudo if needed

The title of this entry tells it all. I've create a simple bash function to prefix any given command expression with sudo if needed. If needed means, if you are not root. It is totally simple but to be true, it took me time to use my bash skills to write it. Imagine you now how to draw a cloud, the sun, a tree and a house and once you figure out how to concatenate all of that elements to draw a picture for your parents ;-).

if [[ $# -lt 1 ]];
then
    echo "Invalid number of arguments provided"
    echo "${FUNCNAME[0]} <command to execute>"
    return 1
fi

if [[ $(whoami) == "root" ]];
then
    $@
else
    sudo $@
fi

Looks like Chuck Norris is using github right now. Github is down, otherwise I would add a link to the fitting repository commit. Github is back online, here is the promised link to the commit.

Trackbacks

Keine Trackbacks

Kommentare

Ansicht der Kommentare: Linear | Verschachtelt

Noch keine Kommentare

Die Kommentarfunktion wurde vom Besitzer dieses Blogs in diesem Eintrag deaktiviert.

Kommentar schreiben

Die angegebene E-Mail-Adresse wird nicht dargestellt, sondern nur für eventuelle Benachrichtigungen verwendet.
Um einen Kommentar hinterlassen zu können, erhalten Sie nach dem Kommentieren eine E-Mail mit Aktivierungslink an ihre angegebene Adresse.

Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss, um dieses Verfahren anzuwenden.
CAPTCHA

Markdown-Formatierung erlaubt
Formular-Optionen