我如何执行shell内置命令与C函数?
我想通过像execv C语言函数来执行Linux命令PWD()。
I would like to execute the Linux command "pwd" through a C language function like execv().
问题是,有没有所谓的PWD的可执行文件,我不能执行回声$ PWD,因为回声也是一个内置的命令,没有可执行程序被发现。
The issue is that there isn't an executable file called "pwd" and I'm unable to execute "echo $PWD", since echo is also a built-in command with no executable to be found.
您应该执行上海-c回声$ PWD
;一般 SH -c
将执行shell命令。
You should execute sh -c echo $PWD
; generally sh -c
will execute shell commands.
(事实上,系统(富)
定义为 EXECL(嘘,嘘,-c,富, NULL)
,因而适用于shell内建的。)
(In fact, system(foo)
is defined as execl("sh", "sh", "-c", foo, NULL)
and thus works for shell built-ins.)
如果你只想值 PWD
,使用 GETENV
,虽然
If you just want the value of PWD
, use getenv
, though.