设为首页 加入收藏

TOP

让你提前认识软件开发(23):如何在C语言中执行shell命令?(二)
2015-01-22 21:34:02 来源: 作者: 【 】 浏览:106
Tags:提前 认识 软件开发 何在 言中 执行 shell 命令
llCommand.o

rm *.o

转到“release”目录下,运行TestShellCommand(有两种运行方式:TestShellCommand和./TestShellCommand),结果如下:

zhou@linux:~/zhouzx/TestShellCommand/release> TestShellCommand

Input the index of source file: 123456

The delete command is: rm -rf TestFile/123456.*

zhou@linux:~/zhouzx/TestShellCommand/release> TestShellCommand

Input the index of source file: 123456.r

The delete command is: rm -rf TestFile/123456.r

zhou@linux:~/zhouzx/TestShellCommand/release> ./TestShellCommand

Input the index of source file: 123456

The delete command is: rm -rf TestFile/123456.*

zhou@linux:~/zhouzx/TestShellCommand/release> ./TestShellCommand

Input the index of source file: 123456.r

The delete command is: rm -rf TestFile/123456.r

上面执行了四次TestShellCommand,123456.r文件都被成功删除掉了。为了验证程序的正确性,可以多用一些文件来进行测试。

六、总结

本文对C语言程序中执行shell命令的方法作了详细的介绍,并用实际的程序代码来演示了其执行结果。

C语言程序执行shell命令的过程中,我们要注意以下问题:

(1) 一定要将shell命令配置正确,并在其执行前后打印详细的日志,方便对程序进行调试和查错。

(2) 在进行删除操作的之前,还要仔细检查shell命令的正确性,确保程序不会误删除。

在实际的软件开发项目中,C语言程序运行于Linux之下已是十分常见。本文介绍了C语言程序中执行shell命令的过程,为相关软件开发工作的顺利开展提供了有益的参考。

(欢迎访问南邮BBS:http://bbs.njupt.edu.cn/)
(欢迎访问重邮BBS:http://bbs.cqupt.edu.cn/nForum/index)

(本系列文章每周更新两篇,敬请期待!本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,微信号:245924426,欢迎关注!)

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇编程精粹--编写高质量C语言代码(.. 下一篇C语言变长数组data[0]

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: