Combine two Bash while loop statements

[亡魂溺海] 提交于 2019-12-25 08:37:47

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/41094178/combine-two-bash-while-loop-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!