General Question

bomyne's avatar

How do I get my bash script to fail if it's being run as root?

Asked by bomyne (639points) May 23rd, 2010
2 responses
“Great Question” (1points)

I’m writing a bash script and I want to make sure it fails to run if the root user is the one running it. How do I do this?

Observing members: 0
Composing members: 0

Answers

davidgro's avatar

#!/bin/bash
if [ "$(id -u)" == "0" ]; then
echo "This script must not be run as root" 1>&2
exit 1
fi

bomyne's avatar

Works perfectly, thank you.

Answer this question

Login

or

Join

to answer.

Mobile | Desktop


Send Feedback   

`