[Tutor] iPython check if user running script is root user (Linux)

Corey Richardson corey at octayn.net
Fri Jun 1 00:30:01 CEST 2012


On Thu, 31 May 2012 18:21:22 -0400
Dave <davestechshop at gmail.com> <davestechshop at gmail.com> wrote:

> Hi. What is the right way to have an iPython script check to see if
> the user is currently root?
> 
> Here's how I do it in bash scripts:
> 
> 
> ########################## CHECK USERNAME PRIVILEGE
> ################################
> 
> if [ $(id -u) != "0" ];then
>        echo "This script must be run as root."
>        exit 1
> fi

import os
if os.environ['USER'] != 'root':
	print('This script must be run as root.')
	exit(1)


-- 
Corey Richardson


More information about the Tutor mailing list