linux - 如何 fork bash 脚本

标签 linux bash shell exec fork

您好,我有一个学校项目,我需要通过子进程使用 exec 创建命令行。我正在用 bash 编写脚本。

这是我的伪代码:

while true; do
read -p "Please type your Linux command. Type q to quit." lnxcmd
case $lnxcmd in
 ([Qq]*) exit;
esac

CREATE FORK
FIND CHILD PROCESS
IN CHILD PROCESS- 
  tokenize into array A, split at " "
  get size of array in n
  aggregate everything from 1 .. n as variable B
  exec A[0], B
display result, and kill child process

循环一直持续到按下 q 为止。因为我没有使用 c,所以我无法创建一个 pid_t 变量并将其分配给 fork() 并像那样进入子进程。

如何使用 bash 命令访问子进程?另外,bash 命令的 exec 的正确语法是什么?

谢谢

最佳答案

在 bash 中,您只需运行一个命令即可 fork/exec 一个子进程。例如,ls 派生一个子进程并执行 ls 的副本。要获取 pid,请在后台运行它:ls& pid=$! – William Pursell

您可以将您的主要工作放在一个函数中,在后台运行它,然后 $! 扩展为 PID。 (嗯,忍者。)– 4ae1e1

关于linux - 如何 fork bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686300/

相关文章:

linux - 使用shell脚本一个接一个地卸载软件

java - Process.getInputStream() 不返回所有行

Linux find重新编码所有文件到子目录

linux - 如何在 bash 脚本中排序?

mysql - 如何将sql文件导入bash脚本

linux - 查找文件路径中没有任何部分不是符号链接(symbolic link)的所有文件

c - 整数常量的无效后缀 "b020000.usdhc"

php - 设置 Apache 。无法写入 var 文件夹

linux - 显示.im6 : no decode delegate for this image format `/tmp/magick-KFcbfWUi'

command-line - 如何测试文件列表是否存在?