必要な例: ここに文字列 fd

必要な例: ここに文字列 fd

man bashこのリダイレクト機能を使用すると、[n]<<<word次のようになります。

The result is supplied as a single string, with a  newline  appended,
to the command on its standard input (or file descriptor n if n is specified).

私は動作しようとしていますが、実際に解決策を見つけることができません。

$ exec 4>out
$ 4<<<asdfwefwef

これは意図した効果がないようです。

これはどのように機能しますか?

ベストアンサー1

難しいのは、fd4で読み取る標準ユーティリティを見つけることです。これは、fd4が次の文字列を取得したことを示しています。

$ ( cat 0<&4 ) 4<<<'Hello, World!'
Hello, World!
$ 

あるいは、stdinやread -u引数を使用せずに文字列をスクリプトに密輸することもできます。

$ read -u 4 FOO 4<<<42 && echo $FOO
42
$ 

実際に読んだ内容はスクリプトの内部に深く埋め込まれており、スクリプトはコマンドラインからfd4リダイレクトを継承します。

$ cat Fd4
#! /bin/bash

#.. Read from stdin
read -r A B C
printf '%s %s %s %s %s\n' $A $B $C $D $E

#.. Read from here string.
read -r -u4 D B E
printf '%s %s %s %s %s\n' $A $B $C $D $E


$ echo stdin gets this | ./Fd4 4<<<'fd4 sees that'
stdin gets this  
stdin sees this fd4 that
$ 

おすすめ記事