bash - Error in a while-loop because of a space -


this question has answer here:

i'm new in business , i've got problem...

in while-loop compare user input variable. problem if user input space or special charakter cant compare it. (in if else think solution while , if else same) here code can see it.

var1="therealpass" counter=0 tries=3 read -sp 'please enter password! (3 tries left): ' pass_var  while (( $pass_var != $var1 || $counter < 2 ))  if [ $pass_var == $var1 ]               echo "that real password! job!"         break        else         counter=$[counter + 1]         tries=$[tries - 1]         if [ $tries == 1 ]                     echo             echo "$tries try left. please try again!"             read -sp 'password: ' pass_var             echo          else             echo             echo "$tries tries left. please try again!"             read -sp 'passwort: ' pass_var        fi fi done 

you have quote string variables. in addition, change while loop syntax following: while [ "$pass_var" != "$var1" ] && [ $counter -lt 2 ]

the double parenthesis constructs using in loop syntax arithmetic evaluations. comparing strings. see the double-parentheses construct

the condition should logical and. in bash, < expressed -lt less comparison. operators

you code becomes this:

var1="therealpass" counter=0 tries=3 read -sp 'please enter password! (3 tries left): ' pass_var  while [ "$pass_var" != "$var1" ] && [ $counter -lt 2 ]  if [ "$pass_var" == "$var1" ]       echo "that real password! job!"     break    else     counter=$[counter + 1]     tries=$[tries - 1]     if [ $tries == 1 ]             echo         echo "$tries try left. please try again!"         read -sp 'password: ' pass_var         echo      else         echo         echo "$tries tries left. please try again!"         read -sp 'passwort: ' pass_var    fi fi done 

further reading: conditional statements


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -