2

以下のようにあるコマンドを実行した際に、その出力をファイルに書き出したいと考えています。
ただ、コマンド実行後に入力を求められ、入力が完了しないと次の処理に進まないような挙動をしています。
このような場合、どう対処すればいいでしょうか。
ご教授お願い致します。

例)

$ command > output.txt

1 件の回答 1

4

command <<< hoge > output.txt のように<<<で1行の入力を受け取る方法や、
echo hoge | command > output.txt のようにパイプで渡す方法でできます。

複数回の入力が必要な場合は、ヒアドキュメント << 終端文字 で複数のコマンドを渡すことができます。

サンプルスクリプト(read_sample.sh)

#!/bin/bash
read -p "Press any key: " DATA
echo "Entered key: $DATA" 
if [ $# -eq 1 ] && [ "$1" = "-two_inputs" ]; then
    read -p "Press next key: " DATA
    echo "next key: $DATA" 
fi

コマンド

$ ./read_sample.sh <<< hoge > output.txt
$ ./read_sample.sh -two_inputs << \\e >> output.txt
fuga
piyo
\e

実行結果(output.txt)

Entered key: hoge
Entered key: fuga
next key: piyo

参考資料

1
  • ご回答ありがとうございます。とても参考になりました。無事疑問が解決いたしました。
    – Naoki
    2022年8月11日 1:24

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。