Results 1 to 8 of 8

Thread: bash scripting

  1. #1
    Join Date
    May 2009
    Location
    ./null --replace
    Beans
    273
    Distro
    Ubuntu 10.04 Lucid Lynx

    bash scripting

    Out of curiosity, I am studying it. =)
    I am having trouble figuring out what's wrong with this script.

    #!/bin/bash
    #This a script that identifies a user whether he/she is a normal, system,
    #or a root user.

    echo -n "Please enter your username: "
    read username

    if sudo grep -q $username /etc/passwd
    then
    id=$(sudo id -u $username)
    declare -i user_id
    user_id=$id

    if [ $user_id -ge 500 ]
    then
    echo "$username, you are a noraml user"
    elif [ $user_id -eq 0 ]
    echo "$username, you are a root user"
    else
    echo "$username, you are a system user"
    fi
    else echo "Sorry, you're not a valid user!"
    fi
    if I run it, the shell gives me this error:
    Please enter your username: frustphil
    ./identify_user.sh: line 16: syntax error near unexpected token `else'
    ./identify_user.sh: line 16: ` else '
    Any idea what's wrong?
    Thanks...=)
    Last edited by frustphil; September 28th, 2009 at 01:37 AM.

  2. #2
    Join Date
    Jun 2009
    Location
    0000:0400
    Beans
    Hidden!

    Re: bash scripting

    Your elif needs a then.

    You also don't need to be sudo to use `id`.

  3. #3
    Join Date
    May 2009
    Location
    ./null --replace
    Beans
    273
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: bash scripting

    Quote Originally Posted by falconindy View Post
    Your elif needs a then.

    You also don't need to be sudo to use `id`.
    So it's the tutorial I followed.. Thanks =)

  4. #4
    Join Date
    Feb 2007
    Location
    Tuxland
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: bash scripting

    The current shell's UID is in the variable $EUID

    Eg:

    Code:
    if [[ $EUID -ne 0 ]]; then
       echo "Error: This script must be run as a superuser. Did you prefix with sudo or are you root?"
       echo 
       exit 1
    fi
    Proud GNU/Linux zealot and lover of penguins
    "Value your freedom or you will lose it, teaches history." --Richard Stallman

  5. #5
    Join Date
    Dec 2006
    Beans
    1,133
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: bash scripting

    Also ubuntu and debian start numbering for regular users at 1000, not 500.
    There are no dumb questions, just dumb answers.

  6. #6
    Join Date
    Sep 2009
    Location
    Freiburg/Germany
    Beans
    1,112
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: bash scripting

    You don't need sudo to access /etc/passwd either (passwords are stored in /etc/shadow this days).

    Code:
    grep -q $username /etc/passwd
    doesn't do what you want here as it finds not only full user names but partial usernames (like "roo") and other text from /etc/passwd (like "bash"), too.
    Either use
    Code:
    grep "^${username}:" /etc/passwd
    or
    Code:
    getent passwd "$username"
    .
    ClassicMenu Indicator - classic GNOME menu for Unity
    Unsettings - configuration program for the Unity
    Privacy Indicator - easily switch privacy settings in Unity
    Arronax - create and modify app starters

  7. #7
    Join Date
    Mar 2008
    Location
    Auckland, New Zealand
    Beans
    6
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: bash scripting

    You forgot the "then" after the elif statement.

    Code:
    elif [ $user_id -eq 0 ]
        then
        echo "$username, you are a root user"
    
    else

  8. #8
    Join Date
    May 2009
    Location
    ./null --replace
    Beans
    273
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: bash scripting

    thanks to all =)

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
  •