1

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?

Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38
opfez
  • 13
  • 3

1 Answers1

2

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
Mathieu
  • 2,679
  • 1
  • 20
  • 25