LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1271|回复: 9

如何在C程序里面调用 bash命令呢?

[复制链接]
发表于 2011-9-3 19:26:09 | 显示全部楼层 |阅读模式
例如,我调用umask会*出current file mode creation mask。
但是我想在C程序里面调用这个umask命令。
就是呢,我写一个C程序,比如test.c,编译,链接后生成test
然后我执行./test
直接*出umask的执行结果。

新手不太懂,请赐教。。
发表于 2011-9-4 09:15:31 | 显示全部楼层

  1. int ret=0;
  2. ret = system("command_name");
  3. /*
  4.   You'd better to check the return value of the function.
  5. */
复制代码


command_name can be any command in LINUX, for example

system("/sbin/ifconfig eth0 172.21.10.24");

For detailed information, you can refer to

man system
回复 支持 反对

使用道具 举报

发表于 2011-9-4 09:18:17 | 显示全部楼层

  1. int ret = system("command");
  2. /*
  3. * You'd better check the return value.
  4. * The command could be any command supported in Linux, for example
  5. * system("/sbin/ifconfig eth0 up");
  6. */

复制代码


For detailed information of system, you can refer to
man system
回复 支持 反对

使用道具 举报

发表于 2011-9-4 09:24:06 | 显示全部楼层

  1. int ret = system("command_str");
  2. /*
  3. * You'd better to check the return value. The command_str could be any command supported in you LINUX, for example,
  4.   * system("/sbin/ifconfig eth0 up");
  5. */
复制代码


For more information about system, you could refer to
man system
回复 支持 反对

使用道具 举报

发表于 2011-9-5 15:33:21 | 显示全部楼层
可以了?
可以了?
回复 支持 反对

使用道具 举报

发表于 2011-9-5 15:34:08 | 显示全部楼层
可以了?可以了?
回复 支持 反对

使用道具 举报

发表于 2011-9-5 15:36:13 | 显示全部楼层
simple example
  1. int ret = system("/sbin/ifconfig eth0 up");
  2. /*
  3. * You'd better check the return value of this call.
  4. * for more info, you can refer to 'man system'
  5. */
复制代码
回复 支持 反对

使用道具 举报

发表于 2011-9-5 15:38:51 | 显示全部楼层
simple example
  1. int ret = system("/sbin/ifconfig eth0 up");
  2. /*
  3. * You'd better check the return value of this call.
  4. * For more info, you can refer to 'man system'
  5. */
复制代码
回复 支持 反对

使用道具 举报

发表于 2011-9-10 23:15:32 | 显示全部楼层
显示有问题?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-9-15 08:00:28 | 显示全部楼层
OK,学习了。。。终于有人回复帖子了。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表