[英]How to store the value returned by a command in shell script?
i am new to shell scripts. 我是Shell脚本的新手。 I have task where i have to create multiple instances of ac program, run them in background and alter their scheduling policy using chrt utility.
我的任务是创建ac程序的多个实例,在后台运行它们,并使用chrt实用程序更改其调度策略。
In chrt utility i need to give the process IDs for every process to alter it's scheduling policy. 在chrt实用程序中,我需要为每个进程提供进程ID,以更改其调度策略。
Now i have to add all these in a shell script to automate the process. 现在,我必须将所有这些添加到shell脚本中以自动化该过程。
My question is, after creating the instances, how do i fetch the process ID of every instance and store it in a variable? 我的问题是,在创建实例之后,如何获取每个实例的进程ID并将其存储在变量中?
gcc example.c -o w1
taskset 0x00000001 ./w1&
taskset 0x00000001 ./w1&
taskset 0x00000001 ./w1&
taskset 0x00000001 ./w1&
pidof w1
chrt -f -p 1 <pid1>
pidof w1 will give the process ids of all the instances. pidof w1将提供所有实例的进程ID。 Now how do i store all these IDs in variables or array and pass them in chrt command?
现在如何将所有这些ID存储在变量或数组中,并在chrt命令中传递它们?
Read this article: https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html 阅读本文: https : //www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html
To store the output of command in a variable: 要将命令的输出存储在变量中:
pids=$(pidof w1)
To use the variable: 要使用变量:
for each in $pids
do
# parse each values and pass to chrt command
chrt -f -p 1 $each
done
You only need pidof
because you ignored the process IDs of the background jobs in the first place. 您只需要
pidof
因为您首先忽略了后台作业的进程ID。
gcc example.c -o w1
pids=()
taskset 0x00000001 ./w1 & pids+=($!)
taskset 0x00000001 ./w1 & pids+=($!)
taskset 0x00000001 ./w1 & pids+=($!)
taskset 0x00000001 ./w1 & pids+=($!)
for pid in "${pids[@]}"; do
chrt -f -p 1 "$pid"
done
The special parameter $!
特殊参数
$!
contains the process ID of the most recently backgrounded process. 包含最新后台进程的进程ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.