入力テキストをアスタリスクに変更

入力テキストをアスタリスクに変更

私はすでにこの質問をしていて、もう一つの質問がありました。これは私のコードです。

#! /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

read -p 'Password:' pass入力のこの部分()をアスタリスクで表示したいと思います。

ベストアンサー1

アスタリスクで入力されたエコ文字? Jon Redが1位を占めましたが、ここに別のものがあります。

#!/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 = $'\177' ]] ; 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"

おすすめ記事