0

I am trying to combine 2 different logical statements in a single while loop, but having trouble getting the logic correct so that 2 different checks can be evaluated in the same loop. For example, I have the following 2 logical statements.

Logic 1

Determine if the entered username is blank and if it is ask the user to re-enter a different username.

echo -ne "User Name [uid]$blue:$reset "
read USERNAME
USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
while [[ -z "$USERNAME" ]]; do
        echo ""
        printf "%s\n" "The User Name CAN NOT be blank"
        echo ""
        echo -ne "User Name [uid]$blue:$reset "
        read USERNAME;
        USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
done

Logic 2

Determine if the read username already exists and if it does ask the user to re-enter a username.

echo -ne "User Name [uid]$blue:$reset "
read USERNAME
USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
$(command -v getent) passwd "$USERNAME" &>/dev/null
while [[ $? -eq 0 ]]; do
        echo ""
        printf "%s\n" "$USERNAME exists in LDAP"
        echo ""
        echo -ne "User Name [uid]$blue:$reset "
        read USERNAME;
        USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
        $(command -v getent) passwd "$USERNAME" &>/dev/null
done

For achieving the described goal I have tried while loops and nested if statements and am just confused at this point. Basically as part of the script I would like these 2 logical statements to be combined when the user is asked to enter a username without the script exiting until a valid value is entered.

2 Answers 2

1

Don't use uppercase variable names!

#!/bin/bash
while true; do
    echo -ne "User Name [uid]$blue:$reset "
    read username
    [ -z "$username" ] && echo -e "\nThe User Name CAN NOT be blank\n" && continue
    username=$(tr [:upper:] [:lower:] <<< $username)
    [ -z $(getent passwd "$username") ] && break || echo -e "\n$username exists in LDAP\n"
done
Sign up to request clarification or add additional context in comments.

1 Comment

I was running into binary operator expected on line 7, but using double [ did the trick, thanks.
0

You could move the conditional check from the while statement to a pair of if statements. In this case, I've also moved the read and related commands to the top of the loop. This means you don't need extra read commands before the loop.

#!/bin/bash
while true; do
    echo -ne "User Name [uid]$blue:$reset "
    read username;
    username=$(echo "$username" | tr "[:upper:]" "[:lower:]")
    $(command -v getent) passwd "$username" &>/dev/null

    if [[ -z "$username" ]]; then
        echo ""
        printf "%s\n" "The User Name CAN NOT be blank"
        echo ""
        continue #Skips the rest of the loop and starts again from the top.
    fi

    if [[ $? -eq 0 ]]; then
        echo ""
        printf "%s\n" "$username exists in LDAP"
        echo ""
        continue #Skips the rest of the loop and starts again from the top.
    fi

    #If execution reaches this point, both above checks have been passed
    break #exit while loop, since we've got a valid username
done

As an aside, it's often recommended to avoid uppercase variable names, in order to avoid conflicts with system environment variables.

1 Comment

Thank you for re-arranging this and adding comments. It makes much more sense to me seeing it structured this way. The shorthand code works, but so does this way. Noted on the uppercase variables you and Ipor mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.