简体   繁体   English

使用exec在c ++中执行shell命令

[英]Executing shell commands in c++ with exec

I've been given the assignment to write a small shell program in C++. 我已经被赋予了用C ++编写一个小shell程序的任务。 It's supposed to take the same commands as a regular bash shell (eg: mv, cmp, etc.) and then use fork() and exec() to call the bash version of the function. 它应该采用与常规bash shell相同的命令(例如:mv,cmp等),然后使用fork()和exec()来调用函数的bash版本。

I've tried a bunch of ways to call the functions, but I keep running into this issue: when the file is in the same directory as the executable it works (eg: "tail test.txt"), but when the file is in a different directory it does not (eg: "tail ~/Documents/test.txt") and it tells me that the file doesn't exist. 我已经尝试了很多方法来调用这些函数,但是我一直在遇到这个问题:当文件与它可以运行的可执行文件位于同一目录中时(例如:“tail test.txt”),但是当文件是它不在另一个目录中(例如:“tail~ / Documents / test.txt”),它告诉我该文件不存在。 The exact wording is: 确切的措辞是:

tail: cannot open '~/Documents/test.txt' for reading: No such file or directory tail:无法打开'〜/ Documents / test.txt'进行阅读:没有这样的文件或目录

but the file does exist, and the same command works in the regular bash shell. 但该文件确实存在,并且相同的命令在常规bash shell中有效。

Now I'm really lost on this, it's supposed to work for files in any directory, and I can't figure out what I'm doing wrong here. 现在我真的迷失了,它应该适用于任何目录中的文件,我无法弄清楚我在这里做错了什么。

This is the relevant code (pretty much identical for all commands): 这是相关代码(几乎所有命令都相同):

pid_t pid = fork();
if(pid > 0)
{
   wait(NULL);
}
else if(pid == 0)
{
    execl("/bin/mv","mv", arg1.c_str(), arg2.c_str(), NULL);
    exit(1);
}

I tried using different versions of exec, but I ran into issues with the commands that require a char *const[] as argument, since the file path is a variable it wouldn't accept it. 我尝试使用不同版本的exec,但是我遇到了需要char *const[]作为参数的命令的问题,因为文件路径是一个它不接受它的变量。

char *const args[] = {"/usr/bin/tail", arg1.c_str(), "-n 5", NULL}; // error here
pid_t pid = fork();
if(pid > 0)
{
    wait(NULL);
}
else if(pid == 0)
{
    execv("/usr/bin/tail", args);
    exit(1);
}

The other versions I've (unsuccessfully) tried are: 我(尝试失败)尝试的其他版本是:

char *env[] = {"PATH=~/"};
execle("/usr/bin/tail", "tail", arg1.c_str(), "-n 5", NULL, env);
execlp("/usr/bin/tail", "tail", arg1.c_str(), "-n 5", NULL);

Any help is very much appreciated! 很感谢任何形式的帮助!

~ is a special character interpreted by the shell, not the filesystem. ~是shell解释的特殊字符,而不是文件系统。 Since you are pretending to be a shell, you need to implement handling for ~ if you want it to work. 由于你假装是一个shell,如果你想让它工作,你需要实现~处理。

For inspiration, you can see how Python implements it (as the function os.path.expanduser() ) here: https://github.com/python/cpython/search?utf8=%E2%9C%93&q=%22def+expanduser%22&type= 为了获得灵感,你可以在这里看到Python如何实现它(作为函数os.path.expanduser() ): https//github.com/python/cpython/search? os.path.expanduser() =% os.path.expanduser() =%22def+ expanduser%22&类型=

If you don't want to implement this, simply pass /home/yourusername instead of ~ to your program. 如果您不想实现此功能,只需将/home/yourusername而不是~传递给您的程序。

Finally, a note: PATH is an environment variable that specifies where to find programs , not files in general. 最后,注意: PATH是一个环境变量,它指定在哪里查找程序 ,而不是一般的文件。 That's why it wasn't useful in your trials. 这就是为什么它在你的试验中没用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM