我在 C 中使用 execv
,但它需要获取命令的路径才能执行它,例如:
- 要执行
ls
,我必须有char* command = "/bin/ls";
- 要执行
gedit
我必须有 char*command = "/usr/bin/gedit";
我的问题是如何在 C 中获取字符串 "/bin"
或 "/usr/bin"
?
最佳答案
which command给出了一个命令的完整路径。例如,
$ which ls
/bin/ls
所以,你可以在 C 程序中做这样的事情,
system ("which ls >x");
// read file x for complete path of ls
关于c - 如何在 C 中获取 Linux 命令位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40060003/