![](/img/trans.png)
[英]how we can call a system function like pwd or ls -l without using system() or exec() function using c or c++ in linux?
[英]How to get 'ls -l $pwd' working using execlp?
我已經從@nos閱讀了以下關於如何實現execlp命令的答案,但是我無法使用execlp為ls -l $pwd
專門運行相同的命令。 我已經嘗試了兩個execlp("ls","ls", "-l", "$pwd", (char *)NULL);
和execlp("ls","ls", "-l", "pwd", (char *)NULL);
但似乎沒有任何效果。 這種思考過程中的任何方向都將非常有幫助
謝謝。
這些$...
變量屬於shell
, 而不是 OS內部。 在shell中鍵入此類變量時,shell會將其轉換為實際值,然后調用系統調用。
在C程序中,您需要自己做:
#include <unistd.h>
int main() {
char *cwd = getcwd(NULL, 0);
execlp("ls","ls", "-l", cwd, (char *)NULL);
}
getcwd()
將為您提供當前目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.