gpt4 book ai didi

linux - 如何 fork bash 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:51:44 26 4
gpt4 key购买 nike

您好,我有一个学校项目,我需要通过子进程使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com