How to hide password input from terminal in ruby script

前端 未结 6 1779
囚心锁ツ
囚心锁ツ 2020-11-30 20:50

I am new to ruby. I need to receive password as an input through gets command.

How do I hide the password input typed in the terminal, during gets

6条回答
  •  渐次进展
    2020-11-30 21:17

    For ruby version 1.8 (or Ruby < 1.9) I used read shell builtin as mentioned by @Charles.

    Putting the code thats just enough to prompt for user name & password, where user name will be echoed to screen while typing in however password typed in would be silent.

     userid = `read -p "User Name: " uid; echo $uid`.chomp
     passwd = `read -s -p "Password: " password; echo $password`.chomp
    

提交回复
热议问题