wsw*_*are 565
这是另一种方法:
#!/bin/bash
# Read Password
echo -n Password:
read -s password
echo
# Run Command
echo $password
Run Code Online (Sandbox Code Playgroud)
这read -s
将为你关闭回声.只需将echo
最后一行替换为您要运行的命令即可.
小智 188
#!/bin/sh
stty -echo
printf "Password: "
read PASSWORD
stty echo
printf "\n"
Run Code Online (Sandbox Code Playgroud)
sme*_*ola 82
一个班轮:
read -s -p "Password: " password
Run Code Online (Sandbox Code Playgroud)
在Linux(和cygwin)下,这个表单适用于bash和sh.但它可能不是标准的Unix sh.
有关更多信息和选项,请在bash中键入"help read".
$ help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.
...
-p prompt output the string PROMPT without a trailing newline before
attempting to read
...
-s do not echo input coming from a terminal
Run Code Online (Sandbox Code Playgroud)
Sus*_*Pal 53
该-s
的选项read
没有在POSIX标准定义.请参见http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html.我想要一些适用于任何POSIX shell的东西,所以我写了一个stty
用来禁用echo 的小函数.
#!/bin/sh
# Read secret string
read_secret()
{
# Disable echo.
stty -echo
# Set up trap to ensure echo is enabled before exiting if the script
# is terminated while echo is disabled.
trap 'stty echo' EXIT
# Read secret.
read "$@"
# Enable echo.
stty echo
trap - EXIT
# Print a newline because the newline entered by the user after
# entering the passcode is not echoed. This ensures that the
# next line of output begins at a new line.
echo
}
Run Code Online (Sandbox Code Playgroud)
此函数的行为与read
命令非常相似.这是一个简单的用法,read
后面跟着类似的用法read_secret
.输入read_secret
显示为空,因为它未回显到终端.
[susam@cube ~]$ read a b c
foo \bar baz \qux
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=bar c=baz qux
[susam@cube ~]$ unset a b c
[susam@cube ~]$ read_secret a b c
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=bar c=baz qux
[susam@cube ~]$ unset a b c
Run Code Online (Sandbox Code Playgroud)
这是另一个使用-r
选项来保留输入中的反斜杠.这是有效的,因为read_secret
上面定义的函数将它接收的所有参数传递给read
命令.
[susam@cube ~]$ read -r a b c
foo \bar baz \qux
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=\bar c=baz \qux
[susam@cube ~]$ unset a b c
[susam@cube ~]$ read_secret -r a b c
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=\bar c=baz \qux
[susam@cube ~]$ unset a b c
Run Code Online (Sandbox Code Playgroud)
最后,这是一个示例,说明如何使用该read_secret
函数以符合POSIX的方式读取密码.
printf "Password: "
read_secret password
# Do something with $password here ...
Run Code Online (Sandbox Code Playgroud)
Man*_*uel 11
我发现这个askpass
命令很有用
password=$(/lib/cryptsetup/askpass "Give a password")
Run Code Online (Sandbox Code Playgroud)
每个输入字符都替换为*.请参阅:提供密码****
您还可以通过执行以下操作来提示输入密码,而无需在当前 shell 中设置变量:
$(read -s;echo $REPLY)
Run Code Online (Sandbox Code Playgroud)
例如:
my-command --set password=$(read -sp "Password: ";echo $REPLY)
Run Code Online (Sandbox Code Playgroud)
您可以使用换行符添加其中几个提示值,执行以下操作:
my-command --set user=$(read -sp "`echo $'\n '`User: ";echo $REPLY) --set password=$(read -sp "`echo $'\n '`Password: ";echo $REPLY)
Run Code Online (Sandbox Code Playgroud)
linux
虽然已经有很多答案,但实际上在任何运行的现代系统中,还有另一种方法可以从终端询问密码systemd
(是的,嘘,我知道,但你能做什么呢)。systemd-ask-password命令包含在标准核心systemd
包中,可以像这样使用:
#!/bin/sh
PASSWORD="$(systemd-ask-password "Enter your password:")"
Run Code Online (Sandbox Code Playgroud)
使用--emoji=no
switch 来抑制那个愚蠢的 unicode 锁定字符。它在终端内工作得很好,但如果您需要它弹出 GUI 对话框,则需要进行调整,但这超出了此处的范围。
归档时间: |
|
查看次数: |
317728 次 |
最近记录: |