[英]How to separate a variable into multiple variable in bash
#! /bin/bash
readarray rows < Process.dat
for ((i=0, j=1; i< ${#rows[@]}; i++, j++)); do
declare "row$j"="${rows[$i]}"
done
echo "$row1"
the output for this code is 10110 556 4433, I have tried using the same piece of code again changing the variables of "rows" and "row" however there is no ouput when i do this, How can i split up the variable "row1" into the 3 seperate numbers each assigned to a different variable, eg row1_1 = 10110, row1_2 = 556, row1_3 = 4433此代码的 output 是 10110 556 4433,我尝试使用相同的代码再次更改“行”和“行”的变量但是当我这样做时没有输出,我怎样才能拆分变量“row1 " 分成 3 个单独的数字,每个数字分配给不同的变量,例如 row1_1 = 10110, row1_2 = 556, row1_3 = 4433
the input file consists of 5 rows and 3 coloumns of numbers:输入文件由 5 行和 3 列数字组成:
10110 556 4433
10120 424 54
12452 123 534
22042 432 12
25321 423 73
the coloumns represent index time, burst time and id number respectively, I need to work out: execution order, average wait time, average completion time列分别代表索引时间,突发时间和id号,我需要计算:执行顺序,平均等待时间,平均完成时间
e.g execution order 4433>54>534>12>73.
average completion time = 6132/5
(556+(556+424)+(556+434+123)+(556+424+123+432)+(556+424+123+432+423)) / 5
average wait time = 4174/5
(0+556+(556+424)+(556+424+123)+(556+424+123+432)) / 5
the endgame is to have each of these numbers assigned to a unique variable that I can later use to do math operations最终的结果是将这些数字中的每一个分配给一个唯一的变量,我以后可以用它来进行数学运算
Assumptions/understandings (from OP's comments):假设/理解(来自 OP 的评论):
index time
, burst time
and id number
输入文件有 3 列: index time
、 burst time
和id number
N
sequentially run processes (where N
is the number of lines in the input file)每行输入包含N
个顺序运行的进程之一的详细信息(其中N
是输入文件中的行数)SUM/N
where SUM
is the sum of execution/wait burst times
for the N
processes (aka number of lines in file)所需的 output 是SUM/N
形式的字符串,其中SUM
是N
个进程的执行/等待burst times
的总和(也就是文件中的行数)ID1>ID2>ID3>...>IDN
which is simply a concatenation of the values in the id number
column另外 output 是ID1>ID2>ID3>...>IDN
形式的字符串,它只是id number
列中的值的串联One bash
idea:一个bash
想法:
sum=0
count=0
comp_total=0
wait_total=0
exec_order=
pfx=
while read -r index burst id
do
exec_order+="${pfx}${id}"
pfx='>'
(( count++ ))
wait_total=$((wait_total + sum))
sum=$((sum + burst))
comp_total=$((comp_total + sum))
done < Process.dat
#echo "comp_total : ${comp_total}"
#echo "wait_total : ${wait_total}"
comp_avg="${comp_total}/${count}"
wait_avg="${wait_total}/${count}"
echo "exec_order : ${exec_order}"
echo "comp_avg : ${comp_avg}"
echo "wait_avg : ${wait_avg}"
This outputs:这输出:
exec_order : 4433>54>534>12>73
comp_avg : 6132/5
wait_avg : 4174/5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.