username and pass login loop bash -


the loop gets initiated though if statement makes no sense, new bash not lot of things make sense me

first=0 echo enter username read user log1=$(grep -q $user username_pass.txt)  echo enter password read pass log2=$(grep -q $pass username_pass.txt )  if [ $log1=0 ] && [ $log2=0 ];     echo welcome     first=1 fi   while [ $log1=1 ] || [ $log2=1 ];      echo wrong user name or password     echo enter username     read user     echo enter password     read pass done  if [ $log1=0 ] && [ $log2=0 ] && [ first=0 ];     echo welcome fi 

[ … ] not particularly special syntax in shells. you’re passing arguments $log1=0 , ] command called [, removes ] arguments , passes rest test. supposing $log1 contains “username”, result is:

test 'username=0' 

when test gets 1 argument, checks if argument non-empty string. $log1=0 never empty string.

what you’re looking pass operator , operands separate arguments, this:

if [ "$log1" = 0 ] && [ "$log2" = 0 ]; 

note quotes, prevent $log1 being expanded more 1 argument.

you meant exit code grep instead of output if you’re comparing against number, $? after running command:

grep -q "$user" username_pass.txt log1=$? 

you split out function ease of use.

test_credentials() {     grep -q "$1" username_pass.txt || return 1     grep -q "$2" username_pass.txt || return 1 }  if test_credentials "$user" "$pass";     … 

you might want rethink how you’re checking credentials. grepping same file twice have many problems, 1 problem definitely has can log in using username password.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -