標準入力から読み出し、次のコマンドでパイプ

標準入力から読み出し、次のコマンドでパイプ

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組み込みコマンド:

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. 

おすすめ記事