Checking for root user in sh and bash [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite













This question already has an answer here:



  • How to stop a script from running if it's not root (and echo “Not running as root! Exiting…”)

    4 answers



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?







share|improve this question














marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 5 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 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 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 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 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 at 6:47














up vote
1
down vote

favorite













This question already has an answer here:



  • How to stop a script from running if it's not root (and echo “Not running as root! Exiting…”)

    4 answers



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?







share|improve this question














marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 5 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 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 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 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 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 at 6:47












up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:



  • How to stop a script from running if it's not root (and echo “Not running as root! Exiting…”)

    4 answers



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?







share|improve this question















This question already has an answer here:



  • How to stop a script from running if it's not root (and echo “Not running as root! Exiting…”)

    4 answers



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?





This question already has an answer here:



  • How to stop a script from running if it's not root (and echo “Not running as root! Exiting…”)

    4 answers









share|improve this question













share|improve this question




share|improve this question








edited Mar 5 at 15:24

























asked Mar 5 at 14:43









Allan

3742516




3742516




marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 5 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.






marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 5 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 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 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 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 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 at 6:47
















  • 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 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 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 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 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 at 6:47















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 at 15:07





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 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 at 15:26




@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 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 at 15:38




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 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 at 15:55





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 at 15:55





1




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 at 6:47




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 at 6:47










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










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




Output only the effective user ID, using the format "%un".




Perhaps like this:



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





share|improve this answer



























    up vote
    0
    down vote













    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.






    share|improve this answer




















    • 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 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 at 14:53

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










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




    Output only the effective user ID, using the format "%un".




    Perhaps like this:



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





    share|improve this answer
























      up vote
      4
      down vote



      accepted










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




      Output only the effective user ID, using the format "%un".




      Perhaps like this:



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





      share|improve this answer






















        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






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




        Output only the effective user ID, using the format "%un".




        Perhaps like this:



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





        share|improve this answer












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




        Output only the effective user ID, using the format "%un".




        Perhaps like this:



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 5 at 14:53









        Jeff Schaller

        31.2k846105




        31.2k846105






















            up vote
            0
            down vote













            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.






            share|improve this answer




















            • 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 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 at 14:53














            up vote
            0
            down vote













            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.






            share|improve this answer




















            • 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 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 at 14:53












            up vote
            0
            down vote










            up vote
            0
            down vote









            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 5 at 14:46









            John

            11.2k11630




            11.2k11630











            • 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 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 at 14:53
















            • 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 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 at 14:53















            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 at 14:49




            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 at 14:49




            1




            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 at 14:53




            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 at 14:53


            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay