【问题标题】:How to deal with a c++ program taking runtime input in bash file? [closed]如何处理在 bash 文件中获取运行时输入的 c++ 程序? [关闭]
【发布时间】:2018-03-08 08:36:48
【问题描述】:

详细来说,我有一个 c++ 程序,它接受不同参数的运行时输入。我正在创建一个 shell 脚本来自动化我的过程。但是程序需要在终端中给出输入。当终端提示输入输入值时,如何从bash发送输入值?

【问题讨论】:

  • 您的意思是要从标准输入(通过std::cin)读取输入,还是将其作为参数传递给程序?

标签: c++ linux bash shell scripting


【解决方案1】:

您可以使用通常的流程(流水线或重定向)为您的程序提供数据

./a.out < myFileWithdata.txt
#or with pipelining
echo "my one line" | ./a.out

另一种选择是使用“here-document”:

./a.out << EOF
My first line!
My second line.
EOF
#more commands

注意: EOF 只是一个约定,它可以是任何用来标记要传递给程序的数据结束的字符串。

另一种选择,快速通过一行是“here-string”:

./a.out <<< "My data!"
#more commands

【讨论】:

  • 感谢您的回复。这很有帮助。
  • 祝你编程好运! :)
猜你喜欢
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2018-07-05
相关资源
最近更新 更多