如何从C在Linux上执行shell脚本? [英] How to execute a shell script from C in Linux?

查看:199
本文介绍了如何从C在Linux上执行shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从C在Linux上执行shell脚本?

How can I execute a shell script from C in Linux?

推荐答案

这取决于你想用脚本(或者你想运行其他程序)做什么。

It depends on what you want to do with the script (or any other program you want to run).

如果你只是想运行脚本系统是最容易做的事情,但它做了一些其他的东西太多,包括运行shell并让它运行命令(如/ bin /下大多数* nix SH)。

If you just want to run the script system is the easiest thing to do, but it does some other stuff too, including running a shell and having it run the command (/bin/sh under most *nix).

如果您想通过其标准输入要么喂shell脚本或使用其标准输出,你可以使用的popen (和函数,pclose ),以建立一个管道。这也采用了壳(如/ bin /下大多数* nix SH)运行命令。

If you want to either feed the shell script via its standard input or consume its standard output you can use popen (and pclose) to set up a pipe. This also uses the shell (/bin/sh under most *nix) to run the command.

这两者都是库函数引擎盖下做了很多,但如果他们不能满足您的需要(或者你只是想尝试和学习),你也可以使用系统直接调用。这也让你避免壳(/ bin / sh的)运行命令你。

Both of these are library functions that do a lot under the hood, but if they don't meet your needs (or you just want to experiment and learn) you can also use system calls directly. This also allows you do avoid having the shell (/bin/sh) run your command for you.

感兴趣的系统调用是的execve waitpid函数。您可能需要使用左右的execve 库包装中的一个(类男子3 EXEC 为他们的列表)。您可能还需要使用其他等待函数( 2人等待有他们全部)。此外,您可能感兴趣的系统调用克隆的vfork 这是关系到餐桌。

The system calls of interest are fork, execve, and waitpid. You may want to use one of the library wrappers around execve (type man 3 exec for a list of them). You may also want to use one of the other wait functions (man 2 wait has them all). Additionally you may be interested in the system calls clone and vfork which are related to fork.

复制目前的方案,其中唯一的主要区别是,新工艺得到0从调用fork返回。父进程获得新的进程的进程ID(或错误)返回。

fork duplicates the current program, where the only main difference is that the new process gets 0 returned from the call to fork. The parent process gets the new process's process id (or an error) returned.

的execve 用一个新的程序(保持相同的进程ID)替换当前的程序。

execve replaces the current program with a new program (keeping the same process id).

waitpid函数用于由父进程等待在一个特定的子进程结束。

waitpid is used by a parent process to wait on a particular child process to finish.

具有叉子和execve的步骤分开允许程序做一些设置为新的进程中创建(不搞乱本身)之前。这些包括改变标准输入,输出,和stderr为比所使用的父进程不同的文件,改变工艺的用户或组,关闭该儿童将不需要的文件,更改会话,或改变的环境变量。

Having the fork and execve steps separate allows programs to do some setup for the new process before it is created (without messing up itself). These include changing standard input, output, and stderr to be different files than the parent process used, changing the user or group of the process, closing files that the child won't need, changing the session, or changing the environmental variables.

您还可能有兴趣在管道 dup2 系统调用。 管道创建一个管道(有一个输入和输出文件描述符)。 dup2 复制文件描述符作为一个特定的文件描述符( DUP 是类似的,但复制一个文件描述符到最低可用的文件描述符)。

You may also be interested in the pipe and dup2 system calls. pipe creates a pipe (with both an input and an output file descriptor). dup2 duplicates a file descriptor as a specific file descriptor (dup is similar but duplicates a file descriptor to the lowest available file descriptor).

这篇关于如何从C在Linux上执行shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆