In bash you can do
if [ $(echo $UID) != 0 ]
then
echo "Run as root"
exit
fi
This returns an error message when I run the script with dash or other POSIX-compliant shells. How do you do this in POSIX?
In bash you can do
if [ $(echo $UID) != 0 ]
then
echo "Run as root"
exit
fi
This returns an error message when I run the script with dash or other POSIX-compliant shells. How do you do this in POSIX?
UID
is not defined for all shells (it's not a POSIX requirement)
You can use id -u
instead:
#! /bin/sh
if [ "$(id -u)" != "0" ]
then
echo "Run as root"
exit
fi
echo
in the sub shell. – Warren Young Jan 14 '20 at 10:27exit 1
as it's an error condition. – Kusalananda Jan 14 '20 at 10:36