C Unix shell:如何检查用户输入以查看它是否';这是一个有效的unix命令吗?
我有一个任务,需要使用fork()创建一个unix shell。我的工作正常。现在我需要检查用户输入,看看它是否是有效的unix命令。如果它是无效的(即它的“1035813”),我需要告诉用户输入一个有效的命令 是否有一种方法可以获取每个可能的unix命令的列表,以便将用户输入与此列表中的每个字符串进行比较?或者有更简单的方法吗?试试看C Unix shell:如何检查用户输入以查看它是否';这是一个有效的unix命令吗?,c,shell,unix,C,Shell,Unix,我有一个任务,需要使用fork()创建一个unix shell。我的工作正常。现在我需要检查用户输入,看看它是否是有效的unix命令。如果它是无效的(即它的“1035813”),我需要告诉用户输入一个有效的命令 是否有一种方法可以获取每个可能的unix命令的列表,以便将用户输入与此列表中的每个字符串进行比较?或者有更简单的方法吗?试试看 if which $COMMAND then echo "Valid Unix Command" else echo "Non valid Un
if which $COMMAND
then echo "Valid Unix Command"
else
echo "Non valid Unix Command"
fi
试试看
if which $COMMAND
then echo "Valid Unix Command"
else
echo "Non valid Unix Command"
fi
您可以检查
的输出,它。如果它在blah/blah
中没有以which:no开头,那么它可能不是该系统上的命令 您可以检查的输出,其中。如果它在blah/blah
中没有以which:no开头,那么它可能不是该系统上的命令 适当的方法是:
检查它是否是shell中的内置命令。例如,cd
可能是一个内置命令
fork
并尝试执行它。(execvp
实际上可能是您真正想要的)。如果失败,请检查errno
以确定原因
例如:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char* argv[])
{
if (argc != 2) {
printf("usage: %s <program-to-run>\n", argv[0]);
return -1;
}
char* program = argv[1];
/* in this case we aren't passing any arguments to the program */
char* const args[] = { program, NULL };
printf("trying to run %s...\n", program);
pid_t pid = fork();
if (pid == -1) {
perror("failed to fork");
return -1;
}
if (pid == 0) {
/* child */
if (execvp(program, args) == -1) {
/* here errno is set. You can retrieve a message with either
* perror() or strerror()
*/
perror(program);
return -1;
}
} else {
/* parent */
int status;
waitpid(pid, &status, 0);
printf("%s exited with status %d\n", program, WEXITSTATUS(status));
}
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
如果(argc!=2){
printf(“用法:%s\n”,argv[0]);
返回-1;
}
char*program=argv[1];
/*在这种情况下,我们不向程序传递任何参数*/
char*const args[]={program,NULL};
printf(“正在尝试运行%s…。\n”,程序);
pid_t pid=fork();
如果(pid==-1){
perror(“未能分叉”);
返回-1;
}
如果(pid==0){
/*孩子*/
if(execvp(程序,参数)=-1){
/*此处设置了errno。您可以使用
*perror()或strerror()
*/
佩罗尔(方案);
返回-1;
}
}否则{
/*母公司*/
智力状态;
waitpid(pid和status,0);
printf(“%s已退出,状态为%d\n”,程序,WEXITSTATUS(状态));
}
}
正确的方法是:
检查它是否是shell中的内置命令。例如,cd
可能是一个内置命令
fork
并尝试执行它。(execvp
实际上可能是您真正想要的)。如果失败,请检查errno
以确定原因
例如:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char* argv[])
{
if (argc != 2) {
printf("usage: %s <program-to-run>\n", argv[0]);
return -1;
}
char* program = argv[1];
/* in this case we aren't passing any arguments to the program */
char* const args[] = { program, NULL };
printf("trying to run %s...\n", program);
pid_t pid = fork();
if (pid == -1) {
perror("failed to fork");
return -1;
}
if (pid == 0) {
/* child */
if (execvp(program, args) == -1) {
/* here errno is set. You can retrieve a message with either
* perror() or strerror()
*/
perror(program);
return -1;
}
} else {
/* parent */
int status;
waitpid(pid, &status, 0);
printf("%s exited with status %d\n", program, WEXITSTATUS(status));
}
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
如果(argc!=2){
printf(“用法:%s\n”,argv[0]);
返回-1;
}
char*program=argv[1];
/*在这种情况下,我们不向程序传递任何参数*/
char*const args[]={program,NULL};
printf(“正在尝试运行%s…。\n”,程序);
pid_t pid=fork();
如果(pid==-1){
perror(“未能分叉”);
返回-1;
}
如果(pid==0){
/*孩子*/
if(execvp(程序,参数)=-1){
/*此处设置了errno。您可以使用
*perror()或strerror()
*/
佩罗尔(方案);
返回-1;
}
}否则{
/*母公司*/
智力状态;
waitpid(pid和status,0);
printf(“%s已退出,状态为%d\n”,程序,WEXITSTATUS(状态));
}
}
如果您想知道它是否是内置命令,您可以滥用帮助:
if help $COMMAND >/dev/null || which $COMMAND >/dev/null
then echo "Valid Unix Command"
else
echo "Not a valid command"
fi
如果您想知道它是否是一个内置命令,您可以滥用帮助:
if help $COMMAND >/dev/null || which $COMMAND >/dev/null
then echo "Valid Unix Command"
else
echo "Not a valid command"
fi
你怎么知道某个UNIX系统某处没有名为“1035813”的命令?你怎么知道某个UNIX系统某处没有名为“1035813”的命令?将perror(程序)添加到我的execvp解决了我的问题。谢谢将perror(程序)添加到我的execvp解决了我的问题。谢谢