読み取りコマンドの-sパラメーターは出力を混乱させます。

読み取りコマンドの-sパラメーターは出力を混乱させます。

だから私のユーザーがスクリプトを再追加し、読み取りコマンドに-s引数がある場合(パスワードを取得するとき)、出力が歪むことがわかりました。

コードは再び次のようになります。

#!/bin/bash
# Only works if you're root

for ((a=1;a>0;a)); do
 if [[ "$UID" -eq 0 ]]; then
  echo "Quit this shit anytime by pressing CTRL + C"
  read -p 'Enter one usernames: ' USERNAME
  nrchar=$(echo ${#USERNAME})
#  echo $nrchar
  spcount=$(echo ${USERNAME} | tr -cd ' ' | wc -c)
#  echo $spcount
  if [[ "${nrchar}" -gt 8 ]]; then
    echo "You may not have more than 8 characters in the username"
   elif [[ "${spcount}" -gt 0 ]]; then
    echo "The username may NOT contain any spaces"
   else
     read -p 'Enter one names of user: ' COMMENT
     read -p 'Enter one passwords of user: ' PASSWORD
     useradd -c "${COMMENT}" -m ${USERNAME}
#     echo "${?}"
     if [[ "${?}" -ne 0 ]]
     then
       echo "Username could not be created"
     else
      echo ${PASSWORD} | passwd --stdin ${USERNAME}
      passwd -e ${USERNAME}
     fi
   fi
 echo "------------------------------------------------------"
 else
  echo "You're not root, so GTFO!"
  a=0
 fi
done

だからこれがここにある間:

     read -p 'Enter one passwords of user: ' PASSWORD

次のように出力されます。

[vagrant@localhost vagrant]$ sudo ./exercise2-stuffs.sh
Quit this shit anytime by pressing CTRL + C
Enter one usernames: user88
Enter one names of user: user
Enter one passwords of user: 88
Changing password for user user88.
passwd: all authentication tokens updated successfully.
Expiring password for user user88.
passwd: Success
------------------------------------------------------
Quit this shit anytime by pressing CTRL + C
Enter one usernames:

これはうまく機能しますが、-sパラメータがある場合:

     read -s -p 'Enter one passwords of user: ' PASSWORD

出力は次のとおりです。

[vagrant@localhost vagrant]$ sudo ./exercise2-stuffs.sh
Quit this shit anytime by pressing CTRL + C
Enter one usernames: user00
Enter one names of user: user
Enter one passwords of user: Changing password for user user00.
passwd: all authentication tokens updated successfully.
Expiring password for user user00.
passwd: Success
------------------------------------------------------
Quit this shit anytime by pressing CTRL + C
Enter one usernames:

だから私の質問はここにあります:

Enter one passwords of user: Changing password for user user00.

これは本当に小さなことであることがわかりますが、なぜこれが起こるのか、そして可能であればどのように避けることができるのかを知るのが良いでしょう。

ありがとうございます。

ベストアンサー1

これがしっかりしているかどうかはわかりませんが、試してみてください。

read -s -p 'Enter one passwords of user: ' PASSWORD; echo

おすすめ記事