read 命令的 -s 参数弄乱了输出

read 命令的 -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

相关内容