我如何执行shell内置命令与C函数? [英] How do I execute a Shell built-in command with a C function?

查看:201
本文介绍了我如何执行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.

这篇关于我如何执行shell内置命令与C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆