在 Bash 脚本中获取进程 ID 并分配给变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16311259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:18:14  来源:igfitidea点击:

Getting a Process ID & Assigning to a Variable in a Bash Script

bashshellunixscripting

提问by JakeGould

I have an interesting situation. All code here is a functional pseudo-code example of the exact issue I am facing so no jokes about assign the output of date. I actually want to capture the output of a slower more resource dependent function, but dateworks well to show the functional obstacle I have run into.

我有一个有趣的情况。这里的所有代码都是我面临的确切问题的功能伪代码示例,所以不要开玩笑分配date. 我实际上想捕获更慢的更多资源依赖函数的输出,但date可以很好地显示我遇到的功能障碍。

I am writing bash script where I want to assign the output of a process to a variable like so:

我正在编写 bash 脚本,我想将进程的输出分配给一个变量,如下所示:

RESPONSE=$(nice -n 19 date);

Now that gives me the RESPONSEin a nice variable, right? Okay, what if I want to get the process ID of the function called within $()? How would I do that? I assumed this would work:

现在这给了我RESPONSE一个不错的变量,对吧?好的,如果我想获取其中调用的函数的进程 ID$()怎么办?我该怎么做?我认为这会奏效:

RESPONSE=$(nice -n 19 date & PID=(`jobs -l | awk '{print }'`));

Which does give me the process ID in the variable PID, but then I no longer get the output sent to RESPONSE.

这确实给了我变量中的进程 ID PID,但是我不再将输出发送到RESPONSE.

The code I am using as a functional example is this. This example works, but no PID; yes I am not assigning a PIDbut again this is an example:

我用作功能示例的代码是这样的。这个例子有效,但没有PID;是的,我没有分配一个,PID但这是一个例子:

RESPONSE=$(nice -n 19 date);
wait ${PID};
echo "${RESPONSE}";
echo "${PID}"; 

This example gives me a PIDbut no RESPONSE:

这个例子给了我一个PID但没有RESPONSE

RESPONSE=$(nice -n 19 date & PID=(`jobs -l | awk '{print }'`));
wait ${PID};
echo "${RESPONSE}";
echo "${PID}";

Anyone know how I can get the value of RESPONSEwith the PIDas well?

任何人都知道我如何获得RESPONSEwith 的价值PID

回答by Gordon Davisson

Depending on exactly how you set it up, using RESPONSE=$(backgroundcommand)will either wait for the command to complete (in which case it's too late to get its PID), or won'twait for the command to complete (in which case its output won't exist yet, and this can't be assigned to RESPONSE). You're going to have to store the command's output someplace else (like a temporary file), and then collect it when the process finishes:

根据您的设置方式, usingRESPONSE=$(backgroundcommand)要么等待命令完成(在这种情况下获取其 PID 为时已晚),要么等待命令完成(在这种情况下其输出不会存在,并且不能分配给 RESPONSE)。您将不得不将命令的输出存储在其他地方(如临时文件),然后在进程完成时收集它:

responsefile=$(mktemp -t response)
nice -n 19 date >$responsefile &
pid=$!
wait $pid
response=$(<$responsefile)
rm $responsefile

(Note that the $(<$responsefile)construct is only available in bash, not in plain posix shells. If you don't start the script with #!/bin/bash, use $(cat $responsefile)instead.)

(请注意,该$(<$responsefile)构造仅在 bash 中可用,在普通 posix shell 中不可用。如果您不使用 启动脚本#!/bin/bash,请$(cat $responsefile)改用。)

This still may not do quite what you're looking for, because (at least as far as I can see) what you're looking for doesn't really make sense. The command's PID and output never exist at the same time, so getting both isn't really meaningful. The script I gave above technically gives you both, but the pid is irrelevant (the process has existed) by the time response gets assigned, so while you havethe pid at the end, it's meaningless.

这可能仍然不能完全满足您的要求,因为(至少就我所见)您正在寻找的东西并没有真正的意义。命令的 PID 和输出永远不会同时存在,因此获取两者并没有真正意义。我在上面给出的脚本在技术上为您提供了两者,但是在分配响应时,pid 无关紧要(该进程已经存在),因此虽然您在最后获得了 pid,但它毫无意义。