1

I have a username system. The user picks a username, password and pin number and this is saved into UPP.db. I want to be able to parse this text so that a user can either be edited (Change password) or Deleted (Remove Line). I have coded a solution for both but both return me with an error and jump to the "Else" part of my If statement. I will provide the code for the edit section as the delete is pretty similar and would be redundant providing both.

echo "Please enter the username you wish to edit"
read usernamecheck

usernamesearch=$( grep $usernamecheck\$ UPP.db | cut -d" " -f1 )

if [ $usernamecheck = $usernamesearch ]
        then
        echo "Please enter $usernamesearch PIN"
                read pincheck
                pinupdate=$( grep $pincheck\$ UPP.db | cut -d" " -f3 )
                if [[ $pin = $pinupdate && $usernamecheck = $usernamesearch ]]
                then
                    echo "The user `grep "$pin"\$ UPP.db | cut -d" " -f1`'s password is about to be changed "
                    echo "Please enter the new password"
                    read newpass
                    passwordcheck=$( grep $pincheck\$ UPP.db | cut -d" " -f2 )
                    sed -i "s/$passwordcheck/$newpass/g" UPP.db
                    echo "Password changed"
                else
                    echo "The PIN is incorrect"
                fi
        else
                echo "This username is not reconised"
fi
;;
3
  • What are the fields of your data file? I'm thinking grep is really the wrong tool here. Commented Dec 3, 2015 at 21:41
  • Quote ALL variables in [ ... ] e.g. (if [ "$usernamecheck" = "$usernamesearch" ]) ... and as a general rule everywhere else except inside [[ ... ]] Also if [[ $pin = $pinupdate && $usernamecheck = $usernamesearch ]] should be if [[ $pin = $pinupdate ]] && [[ $usernamecheck = $usernamesearch ]] Commented Dec 3, 2015 at 21:51
  • @DavidC.Rankin You can use && inside [[...]], just not inside [...] Commented Dec 3, 2015 at 21:57

1 Answer 1

2

string equality comparisons will be more precise than regular expressions. Here, I use awk not grep to enable that. You might want to keep the bash man page handy while you read this.

read -p "Please enter the username you wish to edit: " usernamecheck

# extract all the fields at once
read -r db_user db_pass db_pin < <(
    awk -v name="$usernamecheck" '$1 == name {print; exit}' UPP.db
)

if [[ -z $db_user ]]; then
    echo "This username is not reconised"
else
    read -p "Please enter $db_user PIN: " pincheck
    if [[ "$db_pin" != "$pincheck" ]]; then
        echo "The PIN is incorrect"
    else
        echo "Changing password for user $db_user:"
        while true; do 
            read -s -p "Please enter the new password: " newpass
            echo
            read -s -p "Please re-enter the new password: " newpass2
            echo
            if [[ "$newpass" == "$newpass2" ]]; then
                break
            fi
            echo "Passwords do not match. Try again."
        done

        ln UPP.db "UPP.db.$(date "+%Y%m%d%H%M%S")"    # create a backup copy
        temp=$(mktemp)

        # update the password
        awk -v newpw="$newpass" -v name="$db_user" '
            $1 == name {$2 = newpw}
            {print}
        ' UPP.db > "$temp" && mv "$temp" UPP.db

        echo "Password changed"
    fi
fi

Style tip: in an if-else-end construct, put the smaller block first, then the else-block is close to the condition. Aids readability.

If this plain text file, containing user passwords, is anywhere close to the internet, you're f*cked.

Sign up to request clarification or add additional context in comments.

3 Comments

Works perfectly, Thank you very much. This is not going anywhere near the internet so no worries there, thanks again.
Quick Follow up Question, You have used If [[ -z $db_user ]]; then echo "This username is not reconised" else to check if a username does not exist on the list. Is there an opposite function to -z that can check if a username DOES exist from the same file?
Sure: [[ -n $db_user ]] will succeed if the variable is not empty.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.