我一直在嘗試用 C 中的 execve 函式執行“type”命令
char *arg[] ={
"type",
"type",
NULL
};
execvp("type", arg);
這是我一直在使用的代碼,但它回傳了我 -1 我已經嘗試了與 echo 命令大致相同的代碼,它運行良好
請問有什么幫助嗎?
uj5u.com熱心網友回復:
這是正常和預期的行為:type是內置的 shell,而不是外部命令;execv-family 系統呼叫只能呼叫外部命令。
您可以呼叫一個 shell,并讓該 shell運行type:
/* cmd should be the command you want to check 'type' output for */
char *args[] = { "bash", "-c", "type $@", "bash", cmd, NULL };
...或者您可以which改用(通常功能較弱,但typebash 中添加的額外功能是本質上需要外殼的東西)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422265.html
標籤:
