Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C执行Bash程序?_C_Bash - Fatal编程技术网

如何从C执行Bash程序?

如何从C执行Bash程序?,c,bash,C,Bash,如何使用bash执行命令?系统使用sh而不是bash 我知道我可以使用system/bin/bash-c命令在bash中执行命令。但是我有一个很长的命令,/bin/bash-c给了我很多问题。我需要的是bashruncommand或其他东西 命令是一个字符串,而不是一个文件 在脚本的顶部,然后执行 int status=system("/full/path/to/script"); if(status==-1){ // failure mode } 案例2:存储为字符串的脚本 做下面这样的事情

如何使用bash执行命令?系统使用sh而不是bash

我知道我可以使用system/bin/bash-c命令在bash中执行命令。但是我有一个很长的命令,/bin/bash-c给了我很多问题。我需要的是bashruncommand或其他东西

命令是一个字符串,而不是一个文件

在脚本的顶部,然后执行

int status=system("/full/path/to/script");
if(status==-1){
// failure mode
}
案例2:存储为字符串的脚本

做下面这样的事情

char *command="$(which bash) -c 'ls'";
int status=system(command);
if (status==-1){
  //failure mode
}

如果您要创建一个非常长的shell命令,并且需要bash来解释它,那么您有两个实际选项:

将文本保存到文件中,并将文件名作为单个参数等效地调用bash,在文件中使用shebang指定bash作为解释器,使文件可执行,并将其作为命令调用,或 用popen启动bash实例,并将shell命令作为标准输入写入bash进程。 如果由于shell脚本引用而不是命令长度而出现问题,那么这两个选项中的任何一个都可以工作,或者您可以实现等效的system,但使用execl传递参数而不经过sh。我在这里假设为POSIX类型的system

#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>


int run_bash(const char *command)
{
    int pid = fork();

    if (pid < 0) {
        /* failed */
        perror("fork");
        return pid;
    } else if (pid == 0) {
        /* child */
        execl("/bin/bash", "bash", "-c", command, (char*)NULL);
        perror("exec");
        exit(EXIT_FAILURE);
    } else {
        /* parent */
        int status;
        do {
            waitpid(pid, &status, 0);
        } while (!WIFEXITED(status));
        return WEXITSTATUS(status);
    }
}

int main(void)
{
    run_bash("echo '*'");
}

为什么不在bash脚本中设置shebang,这样你就不必担心运行它的是什么,因为它将始终是bash/bin/bash-f位于顶部。为了确保使用bash,您可以使用:execve/bin/bash、args_数组、environ where args_数组[0]=script.sh您必须为此正确管理内存或类似的方法。必须小心地进入正确的环境。我相信这不是重复——它是问如何使用bash而不是/bin/sh。shebang是错误的。应该是/bin/bash,即使这样,标准还是使用env:/usr/bin/env bash,除非您有非常具体的原因,因为env无法将参数传递给解释器。@Tatsh:谢谢您的指针。我刚才注意到了。同样关于/usr/bin/env bash,这是一个非常好的建议。合并:@Tatsh使用env肯定不是一种规范。有些人喜欢,有些人不喜欢/usr/bin/env bash将使用它在路径中首先发现的任何东西,这可能是一个潜在的安全问题。所以,它不一定比普通的好/bin/bash。我要执行字符串,而不是script@Wilma456:请参阅更新。
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>


int run_bash(const char *command)
{
    int pid = fork();

    if (pid < 0) {
        /* failed */
        perror("fork");
        return pid;
    } else if (pid == 0) {
        /* child */
        execl("/bin/bash", "bash", "-c", command, (char*)NULL);
        perror("exec");
        exit(EXIT_FAILURE);
    } else {
        /* parent */
        int status;
        do {
            waitpid(pid, &status, 0);
        } while (!WIFEXITED(status));
        return WEXITSTATUS(status);
    }
}

int main(void)
{
    run_bash("echo '*'");
}