Find the answer to your Linux question:
Results 1 to 4 of 4
I am currently doing a task for creating users from the command prompt. it is a bash script task with menus etc. now i have created the users and groups ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    10

    If statment help


    I am currently doing a task for creating users from the command prompt. it is a bash script task with menus etc. now i have created the users and groups fine and it works but here is my problem.
    when it prompts me to enter a username and a group and to create the home directory i need to write an if statement that basically says if it exists then don't worry just use it, but if not then create it. here is my script below.

    #!/bin/bash

    HOME=/home/
    SKEL=/etc/skel
    SHELL=/bin/bash
    USERADD=/usr/sbin/useradd
    GROUPADD=/usr/sbin/groupadd

    # Tests to see if root is logged on, if not then message displayed that root muct be logged on
    whoami=`/usr/bin/whoami`
    if [ $whoami != "root" ]; then
    echo "You must be root to add new users"
    exit 1
    fi

    echo
    echo "Add Users Menu"
    echo "**************"
    echo "1) Add User From File"
    echo "2) Add User Manually"
    echo "3) Exit to Main Menu"
    echo
    echo -n "Please select a option (1,2,3): "

    read startmenu
    case "$startmenu" in

    1) clear
    echo "Enter Code"
    ;;

    # Variables entered to create user details
    2) clear
    echo -n "Please Enter Persons Fullname: "
    read Fullname
    echo -n "Please Enter Year: "
    read Year
    echo -n "Please Enter Group: "
    read Group
    echo -n "Please Enter Username: "
    read Username

    # Creates a directory from the Year variable
    mkdir /home/$Year/
    # Creates a directory from the Group variable
    mkdir /home/$Year/$Group/

    # Uses the Groupadd variable to create the group from the group variable
    $GROUPADD $Group

    # Uses the Useradd variable to create the user and directories
    $USERADD -d "/home/$Year/$Group/$Username" -g "$Group" -m $Username
    ;;

    # Exits back to main menu
    3) clear
    ./home.sh
    ;;

    # Any key other then 1,2,3 will return invalid option
    *) clear
    echo "Invalid Option"
    ./addusers.sh
    ;;
    esac

  2. #2
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    First things first: next time, please but your code in a code section, like this:
    Code:
    Code here!
    Now, on to the problem...

    There are two ways you can go about this.

    You can try something like this:
    Code:
    if [ -e /home/$username]; then
    ...
    But you don't really need it for what you're doing. This this instead:

    Code:
    mkdir /home/$username 2> /dev/null
    Mkdir just submits an error message to standard error if the directory exists, so redirect the standard error to /dev/null.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  3. #3
    TBD
    TBD is offline
    Just Joined! TBD's Avatar
    Join Date
    Mar 2006
    Posts
    29
    I would use "else"
    Here is an example of an "else" and "if" statement using true and false.

    if (true) then
    echo "true is true"
    else
    echo "true is false"
    fi

    You can also use the elif command ... but only in the Bourne(sh) shell.

  4. $spacer_open
    $spacer_close
  5. #4
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567

    Arrow

    Code:
    mkdir /home/$username 2> /dev/null
    This certainly may be a "workable solution" of sorts, but it is also very bad programming practice -- better to keep code like that above on the [manual] command-line, and out of scripts -- in a script, it is better to use (and form the good habit of using) a "more proper" way:
    Code:
    if [-e /home/$username]; then
      <code>
    else
      <code>
    fi
    
    OR
    
    if [! -e /home/$username]; then
      <code>
    fi
    
    (depending on what was needed...)
    (Sorry, Javasnob... )

Posting Permissions

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