0

So my user adding script again, I noticed that, if I have the -s argument for the read command (when getting the password), the output gets messed up.

This is the code again:

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

So while this is here:

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

It outputs this:

[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:

Which is good, but if I have the -s argument:

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

The output looks like this:

[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:

So my problem is here:

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

I know it's something really minor, but still, would be nice to know why this happens, and if it's avoidable, how would I go about avoiding it.

Thank you.

0

Not sure if this is rock solid, but try

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.