[shell]system和execlp简单示例

Posted aaronGao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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=$[ $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简单示例的主要内容,如果未能解决你的问题,请参考以下文章

如何在C语言中执行shell命令

Lost connection to MySQL server at 'reading initial communication packet',system error:0(示例代

Lost connection to MySQL server at 'reading authorization packet', system error: 0_Mysql(示例代

倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-点击运行按钮进入到运行状态报错Error starting TwinCAT System怎么办 AdsWarning1823怎么办(示例代

vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0'(示例代

linux cut: invalid byte, character or field list Try 'cut --help' for more information.(示例代