您可以从命令行轻松读取敏感输入吗? [英] Can you portably read sensitive input from the commandline?

查看:54
本文介绍了您可以从命令行轻松读取敏感输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bash 内置的 read 有一个标志 -s 可以防止它回显从命令行读取的任何内容.在搜索opengroup.org并过滤所有其他read 的意义,我还没有找到 POSIX/portable 等价物.有没有合理的方法来做到这一点?

The bash builtin read has a flag -s that prevents it from echoing whatever is being read from the commandline. After searching opengroup.org and filtering through all the other meanings for read, I still haven't found a POSIX/portable equivalent. Is there a reasonable way to do this?

在 bash 中很容易:

In bash it's easy enough:

$ bash -c 'read -sp "What is your password? " password; printf "\n%s\n" "$password"'
What is your password? 
I'll never tell!

但是在 sh...

$ dash -c 'printf "What is your password? "; read password >/dev/null 2>&1; printf "\n%s\n" "$password"'
What is your password? I'll never tell!

I'll never tell!

推荐答案

所以您的问题的答案如本 链接您可以使用内置命令 stty 关闭

So the answer to your question is as described in this link you can turn off by using builtin command stty

stty -echo 

ps:

不要忘记保存您之前的设置

dont forget to save your previous settings

old_set=$(stty -g)
stty -echo

read -r password

stty "$old_set"

这篇关于您可以从命令行轻松读取敏感输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆