简体   繁体   English

在Linux中使用system(“command”)

[英]Using system(“command”) in Linux

Is it recommended to call shell commands from a C program in C using system() ? 是否建议使用system()从C语言中的C程序调用shell命令?

I want to develop a web crawler in C and want to use wget to fetch content from a URL ? 我想在C中开发一个Web爬虫,并希望使用wget从URL获取内容? Should I do it using system(" wget ...") ? 我应该使用系统(“wget ...”)吗?

Regardless of the programming language, calling system shell should really be the last resort when nothing else works, because: 无论使用何种编程语言,调用系统shell都应该是最后的选择,因为:

  • It almost surely will not work under other operating system. 它几乎肯定不会在其他操作系统下工作。
  • The error handling is actually complex (you need to capture and parse the error stream). 错误处理实际上很复杂(您需要捕获并解析错误流)。 Frequently this is just not done and all errors are ignored. 通常这没有完成,所有错误都被忽略了。
  • If you port such an application to another platform, all called tools must be ported as well. 如果将此类应用程序移植到另一个平台,则必须同时移植所有被调用的工具。

The same wget was a problem for us when we were porting some nasty perl code to mobile device. 当我们将一些讨厌的perl代码移植到移动设备时,同样的wget对我们来说是一个问题。 While just from Linux to Linux, we discovered that "standard" wget has some switches BusyBox does not support so needed to port wget as well, and later even grep as if perl would not have its own pattern search. 虽然从Linux到Linux,我们发现“标准” wget有一些交换机BusyBox不支持端口wget也需要,后来甚至grep就好像perl不会有自己的模式搜索。 After that I am fully convinced that better no. 在那之后,我完全相信更好的没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM