Altere o texto para asteriscos na entrada

3

Eu já fiz uma pergunta sobre isso e agora tenho outro. Aqui está o meu código:

#! /bin/bash
read -p 'Username:' name
read -p 'Password:' pass
echo
echo Confirm Username: $name?
echo "Confirm Password: ${pass//?/*}"
echo Let us start the quiz :P 
echo
echo Q1 - Full form of MCQ
echo a - Maximum Capture Quest
echo b - Multiple Choice Question
read -p "Your Answer:" word

if [[ $word == "b" ]]
then
  echo "Correct! V.Good"
else
  echo "Wrong. U Suck"
fi

Eu quero para esta parte ( read -p 'Password:' pass ) a entrada estar em asteriscos.

    
por Rohin 25.06.2017 / 21:04

2 respostas

4

Eco os caracteres inseridos como asteriscos? Jon Red ficou em primeiro lugar, mas aqui está outro:

#!/bin/bash                     

# read a string, prompting using "$1"
# echo characters entered as asterisks
# value is returned in variable 'pass'  
readpw() {              
        printf "%s" "${1-}"
        pass=
        local char
        while IFS= read -r -s -n1 char; do
                if [[ $char = "" ]] ; then
                        # enter, end
                        printf "\n"
                        break
                elif [[ $char = $'7' ]] ; then
                        # backspace, remove one char
                        if [[ $pass != "" ]] ; then
                                pass=${pass%?}
                                printf '\b \b'
                        fi
                else
                        # any other char
                        pass+=$char                 
                        printf "*"
                fi
        done
}

readpw "Enter Password: "
printf "Password entered was: %s\n" "$pass"
    
por 25.06.2017 / 22:17
1

Algo como isso talvez?

#! /bin/bash

read -p 'Username:' name

# read -p 'Password:' pass
unset pass
prompt="Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $'
#! /bin/bash

read -p 'Username:' name

# read -p 'Password:' pass
unset pass
prompt="Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $'%pre%' ]]
    then
        break
    fi
    prompt='*'
    pass+="$char"
done

echo
echo Confirm Username: $name?
echo "Confirm Password: ${pass//?/*}"
echo Let us start the quiz :P
echo
echo Q1 - Full form of MCQ
echo a - Maximum Capture Quest
echo b - Multiple Choice Question
read -p "Your Answer:" word
if [[ $word == "b" ]]
then
  echo "Correct! V.Good"
else
  echo "Wrong. U Suck"
fi
' ]] then break fi prompt='*' pass+="$char" done echo echo Confirm Username: $name? echo "Confirm Password: ${pass//?/*}" echo Let us start the quiz :P echo echo Q1 - Full form of MCQ echo a - Maximum Capture Quest echo b - Multiple Choice Question read -p "Your Answer:" word if [[ $word == "b" ]] then echo "Correct! V.Good" else echo "Wrong. U Suck" fi
    
por 25.06.2017 / 22:05