在c++中实现shell的bash操作符
Implementing bash operators for shell in c++
我正在尝试在我正在制作的bash shell中实现||
, &&
和;
操作符。我要做的是使用int
作为标志,如果成功,则设置为1,如果失败,则设置为0。我的问题是,即使我输入一个无效的操作,如ls -apples
,它将标志设置为1。我还得到错误消息
ls:无效选项——'e'尝试'ls——help'获取更多信息
所以我认为这意味着它在执行?我如何跟踪execvp
是否采取了无效的操作?下面是我的代码:
pid_t pid;
pid_t waitId;
int status;
//forks into two processes
pid = fork();
//There was an error during fork
if (pid < 0)
{
successFlag = 0;
perror("There was an error");
}
else if (pid == 0)
{
//must be cast because our function expects a char *const argv[]
if (execvp(command[0], (char**)command) < 0)
{
//error at execvp
successFlag = 0;
perror("There was an error executing the process");
}
exit(EXIT_FAILURE);
}
else
{
do
{
waitId = waitpid(pid, &status, WUNTRACED | WCONTINUED);
if(waitId == -1){
successFlag = 0;
perror("Error in parent process");
exit(EXIT_FAILURE);
}
}
while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
//use this flag to determine whether the process was a success
successFlag = 1;
解决方案是查看status
返回的数字。这个标志会告诉你是否成功。
相关文章:
- 将bash参数传递给c++
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 终端如何在 shell 提示符下以编程方式阻止退格,以便您无法在其上退格,例如 $ 表示 bash 或 C:\blah\>
- Bash 自动完成一个选项,而不运行应用程序
- 在 Windows 上运行带有 C++ 的 bash 脚本
- 使用 bash 脚本和测试处理进行代码编辑
- 主机箱/bash 未执行 docker 映像
- 如何在 bash 中使用管道在 c++ 中使用 getenv() 访问文件?
- 终端(bash)如何知道引发了什么异常
- -bash:/a.out:无法执行二进制文件:Exec格式错误
- 为什么map有操作符[],set没有
- 在 Bash 脚本中处理来自子进程的信号
- 将字符串变量从 bash 脚本传递到根宏
- 有没有办法在 C 或 C++ 中嵌入 Sh/Bash 会话?
- 如何将C++输出捕获到 Bash 变量?
- Bash run 命令在子壳的后台运行
- 从C++代码中以字符串形式运行多行 bash 脚本
- 在字符串流的 SSH 命令中转义 bash 脚本中的引号
- 如何使用 std::in 和 getline() 检测后台进程中从 bash 注入的输入
- 在c++中实现shell的bash操作符