码迷,mamicode.com
首页 > 系统相关 > 详细

[shell]system和execlp简单示例

时间:2017-05-27 00:42:37      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:脚本   code   logs   shell   oid   exe   span   ash   ram   

shell脚本:hello.sh

#!/bin/bash
echo "i am in shell script"
echo "param 1 is $1"
echo "param 2 is $2"
variable=$[ $1 + $2 ]
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 1 is 10
param 2 is 20
sum is 30

 execlp接口:execlp.c

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

void main(void)
{
    int ret = 0;
    int param1 = 10;
    int param2 = 20;

    printf("i am in c code!\r\n");
    if(fork() == 0)
    {
        printf("execlp...........\r\n");
        ret = execlp("/bin/bash", "bash", "hello.sh", &param1, &param2, NULL);
        if(ret < 0)
        {
             printf("execlp failed!\r\n");
             return;
         }
    }  
}

这其中execlp的参数不管怎么填都得不到正确运行的结果

 

[shell]system和execlp简单示例

标签:脚本   code   logs   shell   oid   exe   span   ash   ram   

原文地址:http://www.cnblogs.com/aaronLinux/p/6910727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!