Friday, September 24, 2010

chkroot

Since I have so many scripts that require root privileges to run, I wrote a simple script to do the check, so all I have to do is add a line in each script to run the check, rather than writing out the same if statement in every script.

Here's the check_root.sh script that I wrote:
#!/bin/bash

if [[ $(id -u) != 0 ]]; then
    echo 'error: this script must be run with root privileges'
    exit 1
fi

exit 0

and here's how it's called within other scripts:
chkroot && [[ $? = 0 ]] || exit 1

No comments:

Post a Comment