1

I am attempting to write a script that automates the installation of ports/packages on new FreeBSD installs. To do this, the user who executes the script must be root.

The system is "supposed" to be virgin meaning bash and sudo may or may not be installed; so I am trying to account for it. To do this, I am checking if the user ID equals 0.

The problem is, between bash and sh, the environment variables are different:

  • bash -> $EUID (all caps)
  • sh -> $euid (all lower)

Is there a different way other than the environment variable to check for root user or should I just adjust the checking of the user based on environment?

marked as duplicate by Kusalananda bash Mar 5 '18 at 15:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • sh on FreeBSD is the Almquist shell, not the Bourne shell. And the euid shell variable that you describe is set by neither the Almquist shell nor the Bourne shell. It is set by the TENEX C shell. You will find it quite difficult to write a script that works on both the Almquist/Bourne Again/Korn/Z shells and the C shell. – JdeBP Mar 5 '18 at 15:07
  • @JdeBP - I learned something new re: Almquist. As for the the varialbes, I am on a virgin FreeBSD install right now (save for bash) and those are the env. variables that are set; bash has all uppers and sh has all lowers. My goal is to write something that's compatible in sh and bash only. – Allan Mar 5 '18 at 15:26
  • You may think that you are running sh. But by the presence of that variable you are clearly not. The Almquist shell, as I said, does not set such a variable. root does not have the Almquist shell as its login shell, note. – JdeBP Mar 5 '18 at 15:38
  • sh on FreeBSD is the Almquist shell and root does not have the Almquist shell as its login. These statements are contradictory. Either root has the Almquist shell in FreeBSD and I'm running it or FreeBSD doesn't use the Almquist shell. I am running FreeBSD and logged in as root using the default sh shell. – Allan Mar 5 '18 at 15:55
  • 1
    No, they are not contradictory at all. root has the TENEX C shell as its login shell, and that does not contradict sh being the Almquist shell at all. As I said, you think that you are running sh. But in fact that is not what you are doing, as the presence of a shell variable set by the TENEX C shell, root's login shell, clearly tells us. – JdeBP Mar 6 '18 at 6:47
5

I would check the value of id -u, which is specified to:

Output only the effective user ID, using the format "%u\n".

Perhaps like this:

if [ $(id -u) -eq 0 ]
then
  : root
else
  : not root
fi
0

It looks like you can simply test for $UID == 0 in both shells. Since the name root isn't the important part, and you can actually have a user named root with a non-zero UID, or a UID 0 account named something other than root, this might be a better way of checking that the script is running as a privileged user.

  • I probably didn't explain it correctly so I'll fix. I'm already checking for a a user ID = 0. The problem is one shell the env. variable is in all caps and the other environment is all lower. – Allan Mar 5 '18 at 14:49
  • 1
    Shell variables can have any value and can not be trusted. I could easily set UID=0 in my personal shell init files, for example. Use id instead. – Kusalananda Mar 5 '18 at 14:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.