Results 1 to 9 of 9

Thread: Bash: Check if sudo

  1. #1
    Join Date
    Nov 2005
    Location
    Denmark
    Beans
    12
    Distro
    Ubuntu 10.04 Lucid Lynx

    Bash: Check if sudo

    This should be a easy question. I'm new to bash-scripting, but I want to play with it a bit.

    I've made a script that can download a big file, and shutdown the computer afterwards, but to make it bulletproof, I want to check if the user wrote sudo in front of it, or else I cant shutdown the computer.

    Code:
    #!/bin/bash
    
    if [[ $1 != "" ]]
    then
            wget -c "$1";
            sudo shutdown -k 0;
    else
            echo "Usage: sudo ./downloadAndShutdown <fileToDownload>";
    fi
    I already check to see if a file has been specified, but still I need to see if the script has the right privileges, and if not only print usage-message.

    Ebbe

  2. #2
    Join Date
    Mar 2006
    Location
    Italy
    Beans
    61
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash: Check if sudo

    Code:
    visi@lappy:~$ whoami 
    visi
    visi@lappy:~$ sudo whoami 
    root
    is enought ?

  3. #3
    Join Date
    Nov 2005
    Location
    Denmark
    Beans
    12
    Distro
    Ubuntu 10.04 Lucid Lynx

    Smile Re: Bash: Check if sudo

    Quote Originally Posted by visik7 View Post
    Code:
    visi@lappy:~$ whoami 
    visi
    visi@lappy:~$ sudo whoami 
    root
    It's perfect
    Thanks.

  4. #4
    Join Date
    Jun 2007
    Beans
    48
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash: Check if sudo

    As a sidenote:

    You could also use
    Code:
    if [ "$(id -u)" != "0" ]; then
    	echo "Sorry, you are not root."
    	exit 1
    fi
    With whoami being the same as id -un
    Code:
    if [ "$(whoami)" != "root" ]; then
    	echo "Sorry, you are not root."
    	exit 1
    fi

    Have fun scripting!
    My new blog Free and Priceless --- It's filled with wholesome Ubuntu goodness!!!

  5. #5
    Join Date
    Jul 2005
    Location
    Ontario, Canada
    Beans
    366
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash: Check if sudo

    Even something like this:

    Code:
    ROOT_UID=0
    
    if [ $UID != $ROOT_UID ]; then
        echo "You don't have sufficient privileges to run this script."
        exit 1
    fi

  6. #6
    Join Date
    Nov 2005
    Location
    Denmark
    Beans
    12
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash: Check if sudo

    Thanks for the nice replies. I think I'm going for
    Code:
    if [ "$(whoami)" != "root" ]; then
    It's easier to read.

    And yes! My script works as expected

  7. #7
    Join Date
    Jul 2006
    Location
    Seattle
    Beans
    93
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Bash: Check if sudo

    just wanted to say that this is a great idea..
    hail the goer

  8. #8
    Join Date
    Nov 2005
    Location
    Denmark
    Beans
    12
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash: Check if sudo

    Just want to show you the final script. The only problem now, is that the file will have root as owner..
    Maybe a chown should be thrown in there.

    Code:
    #!/bin/bash
    
    whoami | grep "root" > /dev/null
    if [ $? -eq 1 ] || [ "$1" = "" ]
    then
    	echo "Usage: sudo $0 <fileToDownload>";
    	exit;
    fi
    
    wget -c "$1";
    shutdown -P +3 "The shutdown can be stopped with: sudo shutdown -c";

  9. #9
    Join Date
    Feb 2007
    Beans
    2,729

    Re: Bash: Check if sudo

    To use grep and produce no output, use the -q option:

    Code:
    $ grep -q root /etc/passwd
    $ echo $?
    0
    No need to run whoami. You can just test the value of the environment variable UID:

    Code:
    $ sudo sh -c 'echo $UID'
    0
    Root UID is always 0. This makes your test:

    Code:
    if [ $UID -eq 0 ] ; then
      ...
    fi
    You can also use "id -u" to get the UID.



    MrC

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •