使用operator <in C从文件重定向

JAN*_*JAN 3 c linux bash io-redirection

bash在C中编写了一个模拟Linux命令的程序.它与键盘的输入完美配合,意思是:

  • application > file:将应用程序的stdout重定向到文件(将输出写入文件)
  • application < file:从文件重定向应用程序的stdin(从文件读取输入)
  • application >> file:将应用程序的stdout重定向到文件(将输出附加到文件)
  • app1 | app2:将app1的stdout重定向到app2的stdin
  • app &:表示应该在后台执行应用程序

当我从键盘输入命令时,所有这些工作都如上所述.

为了完成我的任务,我需要再添加一个元素,即从文件重定向.意思是,如果我的程序被调用bashSimulator,那么如果我这样做:

bashSimulator < fileWithCommands
Run Code Online (Sandbox Code Playgroud)

然后我的程序需要从中获取所有命令fileWithCommands 并执行它们.

我不知道如何从文件重定向.

Ign*_*ams 5

您可以从stdin而不是批处理文件中读取命令,也可以从用户以交互方式读取命令.使用isatty(3)弄清楚,如果是这种情况.