我一直在实现一个使用execvp()执行给定命令的小程序.不使用重定向时它工作正常,但是当我运行如下命令时:
cat file1.txt > redirected.txt
Run Code Online (Sandbox Code Playgroud)
cat输出以下错误消息并失败:
cat: >: No such file or directory
cat: redirected.txt: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我已经做了一些挖掘,我开始认为也许execvp()不允许进行重定向,因为它不在shell中运行.这是否意味着我必须在重定向发生时手动选择并在fork/exec代码中使用管道来解决这个限制?有没有比使用execvp()更好的方法?
谢谢!