- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 C 中的作业编写 shell。现在我唯一的问题是,当我使用 execvp() 调用外部程序然后使用 CTRL + C 关闭时,我发送的任何其他命令都会在提示后打印不在它之前。我制作了一个虚拟程序,它只是一个无限循环来测试它。
因此,关闭该程序后,如果我尝试运行一个不存在的程序,它将打印出来
MINISHELL (cwd) $: 然后是一个空行
然后它会打印我的错误消息,说我输入了一个不存在的程序,然后它不会打印提示,因为它已经存在了。所以它以一个空的新行结束,看起来程序还没有结束,但实际上已经结束了。
它仅在我使用 CTRL + C 关闭非终止程序后才会执行此操作。否则它可以正常执行程序。我是否没有在某处使用 close() 来关闭文件描述符?我不知道。任何帮助表示赞赏。谢谢。
#include "minishell.h"
void intHandler(int sig)
{
my_char('\n');
}
int main(int argc, char **argv)
{
int n;
int pid = 0;
char* s = (char*)malloc(256*sizeof(char));
char cwd[1024];
char** input;
while (1)
{
signal(SIGINT, intHandler);
my_str("MINISHELL:");
if (getcwd(cwd, sizeof(cwd)) != NULL)
my_str(cwd);
else
{
my_str("Error in current directory");
return -1;
}
my_str(" $: ");
n = read(0, s, 256);
s[n] = '\0';
if (n > 1)
{
input = my_str2vect(s);
if (my_strcmp(input[0], "cd") == 0)
{
/*CHANGE DIRECTORY*/
if (input[1] != '\0')
{
if (chdir(input[1]) < 0)
my_str("MINISHELL: Error in path. Make sure the directory exists.\n");
}
else
my_str("MINISHELL: Error. No directory specified.\n");
}
else if (my_strcmp(input[0], "help") == 0)
{
/*HELP*/
my_str("\nMINISHELL COMMANDS:\n\ncd *directory\nChanges the current working directory to *directory\n\nexit\nExits the minishell\n\nhelp\nPrints a help message listing the built in commands\n\n");
}
else if (my_strcmp(input[0], "exit") == 0)
{
/*EXIT*/
my_str("Thank you for using MINISHELL\n");
exit(0);
}
else if (input[0] != NULL)
{
/*EXECUTE AN EXTERNAL PROGRAM*/
if ((pid = fork()) < 0)
my_str("MINISHELL: Error forking\n");
else if (pid > 0)
{
wait(NULL);
}
else
{
if (execvp(input[0], input) < 0)
{
my_str("MINISHELL: Error. Program does not exist in current directory.\n");
}
else
{
exit(0);
close(0);
}
}
}
else
{
my_str("MINISHELL: Error reading command. Type help to see available commands.");
}
}
}
return 0;
}
最佳答案
您不能期望错误消息总是在提示之前打印。 fork 进程与主程序并行运行。两个程序输出到 stdout 的顺序取决于操作系统调度程序运行进程的方式。
我认为大多数真正的 shell(例如 bash)实际上会在执行 fork()
之前检查主进程中存在的文件。或者,您可以使用 pipe()
并手动重定向输出。然后,您可以在执行提示之前强制刷新整个内容。
关于c - C 中的虚拟 Shell 在关闭外部进程后会出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35902836/
我相信我在子 shell 中调用 exit 会导致我的程序继续: #!/bin/bash grep str file | while read line do exit 0 done
我相信我在子 shell 中调用 exit 会导致我的程序继续: #!/bin/bash grep str file | while read line do exit 0 done
我有几个脚本,它们的第一部分看起来是一样的。这部分的功能是识别脚本在哪台机器上运行并相应地设置几个变量。它看起来像这样: ENV=`echo $LOGNAME | cut -c1-8` if
这是我正在尝试做的事情。我有 4 个 shell 脚本。脚本 1 需要先运行,然后是 2,然后是 3,然后是 4,并且它们必须按此顺序运行。脚本 1 需要运行(并在后台等待)2 才能正常运行,但是脚本
我有一个名为 a.sh 的脚本,其中的内容是: //a.sh: #!/bin/bash temp=0 while [ "$temp" -ne 500 ] do echo `date`
在snakemake中,使用shell()函数执行多个命令的推荐方式是什么? 最佳答案 您可以调用shell()多次内run规则块(规则可以指定 run: 而不是 shell: ): rule pro
我有一个 shell 脚本,我向其中传递了一些参数。Test1.sh -a 1 -b 2 -c“一二三” 在 Test1.sh 中,我按以下方式调用另一个 shell 脚本。Test2.sh $* 我
我有 2 个 shell 脚本。 第二个shell脚本包含以下函数第二个.sh func1 func2 first.sh 将使用一些参数调用第二个 shell 脚本, 将使用特定于该函数的一些其他参数
我有一个 Unix shell 脚本 test.sh。在脚本中,我想调用另一个 shell,然后从子 shell 执行 shell 脚本中的其余命令并退出 说清楚: test.sh #! /bin/b
我想在 shell 脚本中更改路径环境变量。路径变量需要在shell脚本执行后修改。 最佳答案 我知道有两种方法可以做到这一点。第一种是在当前 shell 的上下文中运行脚本: . myscript.
此 shell 脚本按预期运行。 trap 'echo exit' EXIT foo() { exit } echo begin foo echo end 这是输出。 $ sh foo.sh
我正在使用 vimshell在 vim 中执行命令 nnoremap vs :VimShellPop 使用此键映射,我可以打开 vim shell 并执行诸如“捆绑安装”之类的命令,然后 输入 exi
我想连接到不同的 shell(csh、ksh 等)并在每个切换的 shell 中执行命令。 下面是反射(reflect)我的意图的示例程序: #!/bin/bash echo $SHELL csh e
我目前正在尝试使用 BNF 和 LL 解析器在 C 中重新编写 shell。 否则,我需要知道 shell 运算符的优先级是什么| , > , > , & , ; ? 有没有人可以提供给我? 谢谢 最
不幸的是,我没有suspend 命令(busybox/ash)。但是我可以使用 kill -STOP $$ 从后台 shell (sh &) 返回到父 shell(以及 fg 之后)。 但是我不想输入
我需要知道,当用户切换到另一个 shell 时,通过单击它。 我试过 shellListener.shellDeactivated()但是当 shell 失去对它自己的控件的焦点时,会触发此事件,这意
file1.txt aaaa bbbb cccc dddd eeee file2.txt DDDD cccc aaaa 结果 bbbb eeee 如果能不区分大小写就更好了! 谢谢! 最佳答案 gre
我见过解压缩目录中所有 zip 文件的循环。但是,在运行此之前,我宁愿确保我将要运行的内容正常工作: for i in dir; do cd $i; unzip '*.zip'; rm -rf *.z
我对编程还很陌生,但我想知道 vim、emacs、nano 等 shell 文本编辑器如何能够控制命令行窗口。我主要是一名 Windows 程序员,所以可能在 *nix 上有所不同。据我所知,只能将文
我有一个包含第 7 列日期的文件,我的要求是将它与今天的日期进行比较,如果小于它,则删除该完整行。 此外,如果第 7 列中提到的任何日期超过 15 天,则将其修改为最多 15 天 下面的例子- now
我是一名优秀的程序员,十分优秀!