Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I'm trying to write a simple script. Here is what I have so far: #! /bin/bash echo "$USERNAME Please Login With Password!!" COUNT=5 while [ COUNT > 0 ] ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    4

    Question Loop Help

    Hello,

    I'm trying to write a simple script. Here is what I have so far:
    #! /bin/bash
    echo "$USERNAME Please Login With Password!!"
    COUNT=5
    while [ COUNT > 0 ] ; do
    echo "Login Name: $USERNAME "
    echo "Password:"
    read Psword
    if [ $PASSWORD = $PSWORD ] ; then
    echo "Login Successful"
    let COUNT=COUNT-5
    else
    if [ $PASSWORD != $PSWORD ] ; then
    echo "Wrong Password!!"
    let COUNT=COUNT-1
    fi
    fi
    done

    Basically what I'm trying to do is to compare variables $PASSWORD to $PSWORD. If they are equal the just echo "Login Good" if they are not the same, then echo "Wrong Password." Now the part I can't figure out. I want to limit the number of failed attempts to five. So basically limit the endless while loop to only five go arounds. I've been trying to mess with this COUNT thing, but I don't know if it's what I need or not.

    Any Help Greatly apreciated!!!!!

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    The '>' usually redirects output. If you look in your directory you'll find a file named 0. To make bash use '>' as 'greater than', 'escape' it by preceeding it with a backslash(\).

    while [ $COUNT \> 0 ] ; do

    or use -gt

    while [ $COUNT -gt 0 ] ; do

    Note that I preceeded COUNT with a'$' in order to get the value of COUNT.

    Also, variables are case sensitive so Psword in your 'read' command isn't the same as PSWORD is your 'if' statements.

    Finally, when comparing strings in 'if' statements enclose them in double quotes.

    if [ "$PASSWORD" = "$PSWORD" ] ; then

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    4

    Works!!!

    Thanks so much for the help. It works and does exactly what I was trying to get it to do. Here is the final result:

    #! /bin/bash
    echo "$USERNAME Please Login With Password!!"
    COUNT=5
    while [ COUNT -gt 0 ] ; do
    echo "Login Name: $USERNAME "
    echo "Password:"
    read PSWORD
    if [ $PASSWORD = $PSWORD ] ; then
    let COUNT=COUNT-5
    echo "Login Successful"
    else
    if [ $PASSWORD != $PSWORD ] ; then
    let COUNT=COUNT-1
    echo "Wrong Password!!"
    fi
    fi
    done

Posting Permissions

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