Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, I have a question regarding a bash script. Code: #ask for first name and assign to firstName echo -e "\nWhat is your First name? " read firstName I would ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    3

    N00B Bash script question

    Hello, I have a question regarding a bash script.

    Code:
    #ask for first name and assign to firstName
    echo -e "\nWhat is your First name? "
    read firstName
    I would like to add and if statement like this

    Code:
    if [firstName == ""]
    then
    echo -e "\nPlease enter a name."
    My question is how do I come out of the if statement and back to the "what is your name?"

    Sorry if this is unclear or if the syntax isn't correct. I only have a small amount of coding knowledge and its Java...

    Thanks in advance!!

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    You might want to use a while loop:

    Code:
    firstname=""
    while [ "$firstname" == "" ]; do
            echo -ne "\nWhat is your First name? "
            read firstname
            if [ "$firstname" == "" ]; then
                    echo "Please enter name."
            fi
    done
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    3
    Quote Originally Posted by matonb View Post
    You might want to use a while loop:

    Code:
    firstname=""
    while [ "$firstname" == "" ]; do
            echo -ne "\nWhat is your First name? "
            read firstname
            if [ "$firstname" == "" ]; then
                    echo "Please enter name."
            fi
    done

    Ah of course! Now that I see it, it seems so simple. I guess that's the way it goes when you're learning though. Thanks!!

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    Absolutly, and of course as with most programming there are lots of ways to get things done....
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

Posting Permissions

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