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

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

问题描述

如何在 Linux 中从 C 执行 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).

如果你只想运行脚本 system 是最容易做的事情,但它也做一些其他的事情,包括运行一个 shell 并让它运行命令(大多数 *nix 下的/bin/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)来设置管道.这也使用shell(大多数*nix 下的/bin/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.

这两个都是在幕后做了很多事情的库函数,但是如果它们不能满足您的需求(或者您只是想尝试和学习),您也可以直接使用系统调用.这也使您可以避免让 shell (/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.

感兴趣的系统调用是forkexecvewaitpid.您可能想要使用 execve 周围的库包装器之一(键入 man 3 exec 以获得它们的列表).您可能还想使用其他等待函数之一(man 2 wait 拥有它们).此外,您可能对与 fork 相关的系统调用 clonevfork 感兴趣.

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.

fork 复制当前程序,其中唯一的主要区别是新进程从对 fork 的调用返回 0.父进程获取返回的新进程的进程 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.

将 fork 和 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.

您可能还对 pipedup2 系统调用感兴趣.pipe 创建一个管道(带有输入和输出文件描述符).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).

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

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