Score:0

How I can add to variable values in while loop using read line?

vn flag

I need to do a password input bash script. Bash file must get values from named piped until it receives *.

For example:
Bash gets 1 2 5 6 7 8 ... 5 * (Each digit comes after press on keypad)

Variable in script must be: Result = 125678...5
And then I have to compare it to my Verif. password

For example(I dont know bash syntax):

while read line && input != "*"
do
passWord_input += line


if(passWord_input == verifPassword)
    echo "access granted"
else
    echo "access denied
Score:0
cn flag

Don't try to reinvent the wheel:

passWord_input=""
while [[ "$passWord_input" != "$verifPassword" ]]; do
    read -s -r -p "Enter Password: " passWord_input
done

echo "valid password entered"

See https://www.gnu.org/software/bash/manual/bash.html#index-read

KeoFoxy avatar
vn flag
This code above works not the way I need. It accepts all password digits at once but I need to do it like ATM - one digit by another and when I face the * sign stop the loop and check if inputed password is correct
Score:0
hr flag

I can't think of a good reason to do this (except perhaps as a homework exercise), but yes, in bash, you can "add variables in a while loop using read". In particular, man bash notes of the += operator:

   In  the context where an assignment statement is assigning a value to a
   shell variable or array index, the += operator can be used to append to
   or  add  to  the variable's previous value.  
   .
   .
   .                                                             When  ap‐
   plied  to  a  string-valued variable, value is expanded and appended to
   the variable's value.

So for example:

#!/bin/bash

declare +i password_input=""
while IFS= read -s -r -n 1 c; do 
  case $c in 
    \*) break
        ;; 
     *) password_input+=$c
        ;; 
  esac
done

The -n 1 arguments allow you to read single characters rather than whole lines. Unsetting the IFS variable allows the input to contain characters that would otherwise be consumed by field splitting (which may or may not be a good idea in the case of the default IFS - should passwords be allowed to contain whitespace?).

The explicit +i declaration doesn't seem to be required even if the user enters a wholly numeric sequence, but it makes it clear that the intent is to append rather than sum.

FYI the bash syntax for testing string equality is [[ $str1 == $str2 ]] .

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.