【问题标题】:Bash Script Command Line Argument Batch Stdin (heredocs)Bash 脚本命令行参数批处理标准输入(heredocs)
【发布时间】:2014-03-28 04:23:47
【问题描述】:

我有一个程序在启动几秒钟后要求输入标准输入。我想从命令行发送标准输入,而不是手动输入。我做了以下事情:

./program.sh << EOF
./program.sh <<< EOF
./program.sh << 'input'
./program.sh <<< 'input'

但没有任何工作。为什么 heredocs 在这种情况下不起作用?

【问题讨论】:

  • echo 'input' | ./program.sh 工作吗?
  • 第一种方法应该有效。你是怎么尝试的?

标签: bash shell stdin


【解决方案1】:

&lt;&lt; 表示here document

./program.sh <<END
hello
world
END

&lt;&lt;&lt; 表示here string

./program.sh <<< "hello world"

如果输入以某种方式生成,请使用管道或从 process substitution 重定向

helloWorldGenerator.sh | ./program.sh
./program.sh < <(helloWorldGenerator.sh)

【讨论】:

    【解决方案2】:

    程序可能认为自己比实际更具交互性,并忽略未在正确时间到达的输入。 telnet 为此而臭名昭著。选项有:

    1. 好办法:使用expect交互式等待提示并输入响应。

    2. 坏方法:尝试计时响应并希望它有效,

    例如

    { sleep 5; echo "foo"; sleep 5; } | ./program.sh
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      相关资源
      最近更新 更多