[shell]system和execlp简单示例
shell脚本:hello.sh
#!/bin/bash
echo "i am in shell script"
echo "param 1 is $1"
echo "param 2 is $2"
variable=$[ $ + $ ]
echo "sum is $variable"
system接口:system.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> void main(void)
{
printf("i am in c code!\r\n"
system("/bin/bash ./hello.sh 10 20);
}
输出:
i am in c code!
i am in shell script!
param is
param is
sum is
execlp接口:execlp.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h> void main(void)
{
int ret = ;
int param1 = ;
int param2 = ; printf("i am in c code!\r\n");
if(fork() == )
{
printf("execlp...........\r\n");
ret = execlp("/bin/bash", "bash", "hello.sh", ¶m1, ¶m2, NULL);
if(ret < )
{
printf("execlp failed!\r\n");
return;
}
}
}
这其中execlp的参数不管怎么填都得不到正确运行的结果
使用system获取system本身的返回值和脚本的返回值
int system_func(const char *command)
{
int status;
int ret; status = system(command);
#ifdef DEBUG
printf("status is 0x%x\n", status);
#endif
if(- == status)
{
printf("%s:system error\n", __FUNCTION__);
return -;
}
else
{
if(WIFEXITED(status))
{
if( == WEXITSTATUS(status))
{
ret = (status>>)&0xFF;
return ret;
}
else
{
printf("%s:run shell script fail, script exit code:%d\n", __FUNCTION__, WEXITSTATUS(status));
return -;
}
}
else
{
printf("%s:system fail, exit status=%d\n", __FUNCTION__, WEXITSTATUS(status));
return -;
}
}
}
[shell]system和execlp简单示例的更多相关文章
- akka简单示例-1
刚刚开始接触akka,网上找了2个简单示例,并在公司运营机器上尝试,踩了一些坑,在此记录. 1. 本地hello world [torstan@sparkb5-i ~/akka_example/hel ...
- 【Apache Kafka】二、Kafka安装及简单示例
(一)Apache Kafka安装 1.安装环境与前提条件 安装环境:Ubuntu16.04 前提条件: ubuntu系统下安装好jdk 1.8以上版本,正确配置环境变量 ubuntu系统下安 ...
- SignalR 简单示例
一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...
- Web API 简单示例
一.RESTful和Web API Representational State Transfer (REST) is a software architecture style consisting ...
- JAVA使用jdbc连接MYSQL简单示例
以下展示的为JAVA使用jdbc连接MYSQL简单示例: import java.sql.DriverManager; import java.sql.ResultSet; import java.s ...
- Mybatis的简单示例
首先新建一个JavaWeb项目并导入mybatis依赖的jar包,同时Mybatis是对数据库的操作所以我们需要在数据库中新建一个表user用来演示. 新建完表之后我们还需要建立相对应的实体类User ...
- [xfire]使用xfire开发webservice的简单示例
目前项目上有用到xfire,所以临时看了些xfire的资料和示例,自己照着写了一个简单示例. xfire在2007年后已经停止更新,正式更名为apache cxf,也可以说是xfire2.0. xfi ...
- Redis 安装与简单示例
Redis 安装与简单示例 一.Redis的安装 Redis下载地址如下:https://github.com/dmajkic/redis/downloads 解压后根据自己机器的实际情况选择32位或 ...
- Java调度框架Quartz简单示例
Quartz的大名如雷贯耳,这里就不赘述,而且本文也不作为深入探讨,只是看完Quartz的官方文档后,下个简单示例,至少证明曾经花了点时间学习过,以备不时之需. Quartz使用了SLF4J,所以至少 ...
随机推荐
- JVM垃圾回收(GC)整理总结学习
基本回收算法 1. 引用计数(Reference Counting)比较古老的回收算法.原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数.垃圾回收时,只用收集计数为0的对象.此算法最 ...
- 【Web】Rest && 权限管理 && LDAP && OAuth && Nginx && Lua 等
最好的8个 Java RESTful 框架:http://www.importnew.com/17138.html 如何设计RESTful的API权限:https://segmentfault.com ...
- 修改ubuntu下网卡名不是eth0的问题
sudo nano /etc/default/grub 找到GRUB_CMDLINE_LINUX=""改为GRUB_CMDLINE_LINUX="net.ifnames= ...
- function/bind
1.函数指针指向一类函数,这类函数的类型一样,也就是函数的返回类型和形参表一样. 2.不同的函数类型要使用不同的函数指针,才能指向它,有没有好的办法呢? 类比思考下,交换方法,对不同的类型要写不同的s ...
- css 小问题解决方法整理
1,图片垂直居中: 设置包括图片的div:height=20px:line-height=20px. 设置图片vertical-align:middle 就可以. 2,行内块元素有3px bug,可通 ...
- Absolute positioning
The programmer specifies the position and the size of each widget in pixels. When you use absolute p ...
- view xml 中的 button 调用web客户端事件
最近写一个模块 需要 在客户端干点事. 按常规的方法, 应该是写个 客户端模块. 在 客户端 init, start, render 去渲染个按钮出来干事.暂时还不太理解WEB模块如何很好地同服务器端 ...
- html <br/>引起的”血案“
图片一: 图片二: 图片三: 图片四:
- centos增加软连接
#增加软连接 ln -s /usr/local/git/bin/* /usr/bin/
- 火狐浏览器(firefox)中js要注意的问题
1.出现错误调用error方法或者ajax请求了多次,那么极有可能是异步请求的原因. 添加 async : false , -> async. 默认是 true,即为异步方式,$.aja ...