BASH Scripting: Check If running with sudo/superuser, if not, dont run, return error
        Posted  
        
            by 
                EvilPhoenix
            
        on Ask Ubuntu
        
        See other posts from Ask Ubuntu
        
            or by EvilPhoenix
        
        
        
        Published on 2011-03-13T07:29:06Z
        Indexed on 
            2011/03/13
            8:17 UTC
        
        
        Read the original article
        Hit count: 549
        
This is something I've been curious about.  I make a lot of small bash scripts (.sh files) to do tasks that I routinely do.  Some of those tasks require everything to be ran as superuser.  I've been curious:  Is it possible to, within the BASH script prior to everything being run, check if the script is being run as superuser, and if not, print a message saying You must be superuser to use this script, then subsequently terminate the script itself.  The other side of that is I'd like to have the script run when the user is superuser, and not generate the error.
Any ideas on coding (if statements, etc.) on how to execute the aforementioned?
© Ask Ubuntu or respective owner