简体   繁体   English

如何使用execlp使'ls -l $ pwd'工作?

[英]How to get 'ls -l $pwd' working using execlp?

I have read the following answer from @nos on how to implement the execlp command but I am unable to run the same specifically for ls -l $pwd using execlp. 我已经从@nos阅读了以下关于如何实现execlp命令的答案,但是我无法使用execlp为ls -l $pwd专门运行相同的命令。 I have tried both execlp("ls","ls", "-l", "$pwd", (char *)NULL); 我已经尝试了两个execlp("ls","ls", "-l", "$pwd", (char *)NULL); and execlp("ls","ls", "-l", "pwd", (char *)NULL); execlp("ls","ls", "-l", "pwd", (char *)NULL); but nothing seems to work. 但似乎没有任何效果。 Any directions in this thought process would be really helpful 这种思考过程中的任何方向都将非常有帮助

Thanks. 谢谢。

Those $... variable belongs to shell , not the OS internal. 这些$...变量属于shell而不是 OS内部。 When in shell you type such variables, shell will transform them to their actual value and then call system call. 在shell中键入此类变量时,shell会将其转换为实际值,然后调用系统调用。

In C program, you need to do it by yourself: 在C程序中,您需要自己做:

#include <unistd.h>

int main() {
    char *cwd = getcwd(NULL, 0);
    execlp("ls","ls", "-l", cwd, (char *)NULL);
}

getcwd() will give you the current directory. getcwd()将为您提供当前目录。

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

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