从标准输入读取并通过管道传递到下一个命令

从标准输入读取并通过管道传递到下一个命令

我想从 stdin 读取密码,抑制其输出并使用 base64 对其进行编码,如下所示:

read -s|openssl base64 -e

正确的命令是什么?

答案1

read 命令设置 bash 变量,它不会输出到 stdout。

例如,将 stdout 放入 Nothing1 文件,将 stderr 放入 Nothing2 文件,您将在这些文件中看不到任何内容(带或不带 -s arg)

read 1>nothing1 2>nothing2 
# you will see nothing in these files (with or without -s arg)
# you will see the REPLY var has been set
echo REPLY=$REPLY

所以你可能想做类似的事情:

read -s pass && echo $pass |openssl base64 -e
# Read user input into $pass bash variable.
# If read command is successful then echo the $pass var and pass to openssl command.

从 man bash SHELL BUILTIN COMMANDS 读取命令:

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
          One  line  is read from the standard input, or from the file descriptor fd supplied as an argument to the -u option, and the first word is
          assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening  separators  assigned
          to  the  last  name.  

    -s     Silent mode.  If input is coming from a terminal, characters are not echoed.

    If  no  names  are supplied, the line read is assigned to the variable REPLY. 

相关内容