簡體   English   中英

使用 exec 在 C 的不同目錄中執行 ls

[英]Executing ls in a different directory in C using exec

標題對我想要做什么是不言自明的。

我目前的嘗試:

  chdir("/Path/I/want/");   //this is different that the path my program is located at
      char * ls_args[] = { "ls" , "ls -l", NULL};
      execv(ls_args[0], ls_args);
    }

我沒有收到任何錯誤或 output,有什么幫助嗎?

Execv function 需要您必須執行的命令的完整路徑。 因此,您應該找出 ls 在系統中的位置,而不是給出"ls"

$ which ls

它可能在 /bin 文件夾中。 所以你必須把"/bin/ls"給exec。 ls 的任何參數也應該是數組中的另一個成員。 所以而不是使用

char * ls_args[] = { "ls", "ls -l", NULL};

利用

char * ls_args[] = { "/bin/ls", "-l", NULL};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM