0
我试图重新创建一个像tcsh一样的基本C Shell,但我理解变量$status
。这是地位tcsh
一个例子:
存在的命令:
$> pwd
/home/user
$> echo $status
0
的命令不存在:
$> foo
foo: Command not found.
$> echo $status
1
什么的状态值是指什么?从tcsh返回值?
我试图重新创建一个像tcsh一样的基本C Shell,但我理解变量$status
。这是地位tcsh
一个例子:
存在的命令:
$> pwd
/home/user
$> echo $status
0
的命令不存在:
$> foo
foo: Command not found.
$> echo $status
1
什么的状态值是指什么?从tcsh返回值?
$status
或$?
表示以前启动的命令的退出状态。更准确地说,子进程的退出状态。因为在不存在命令的情况下,有一个分叉的子shell,但它不能执行exec()
该命令。 ,或通过从`main`函数返回
int pid = fork(); if (pid == 0) { /* Child shell process */ /* Try to replace child shell with cmd, in same child PID */ /* cmd will generate the exit status of child process */ execve(cmd, argv, envp); /* If execve returns, it's always an error */ /* Child shell generates exit status for error */ write(2, "command not found\n", 18); exit(127); } else { /* Parent shell process */ /* Original shell waits for child to exit */ int status; wait(&status); /* Assuming only one child */ /* this is accessible in shell as $status or $? */ status = WEXITSTATUS(status); }
你知道你可以使用'exit'功能的C程序退出:
壳通常开始这样的命令?您传递给'exit'或与'return'语句一起使用的值是进程的退出代码,返回并用于'$ status' tcsh变量的值。 tcsh也有一些特殊的状态,就像没有找到程序时使用的一样(比如你的例子)。在特殊情况下使用和设置的状态取决于您。 –
http://www.tcsh.org/tcsh.html/Special_shell_variables.html#status –
是的,我意识到这一点,但由于我正在重新创建一个shell解释器,我将使用无限循环来运行我的代码并等待用户输入,如果我从主返回值或使用退出函数它将终止我的程序 – James