简体   繁体   English

如何在 bash 中将一个变量分成多个变量

[英]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 的评论):

  • input file has 3 columns: index time , burst time and id number输入文件有 3 列: index timeburst timeid number
  • each line of input contains details for one of the N sequentially run processes (where N is the number of lines in the input file)每行输入包含N个顺序运行的进程之一的详细信息(其中N是输入文件中的行数)
  • desired output are strings of the form 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形式的字符串,其中SUMN个进程的执行/等待burst times的总和(也就是文件中的行数)
  • additional output is a string of the form ID1>ID2>ID3>...>IDN which is simply a concatenation of the values in the id number column另外 output 是ID1>ID2>ID3>...>IDN形式的字符串,它只是id number列中的值的串联
  • OP has stated these outputs will not be used in follow-on 'math operations' OP 已声明这些输出不会用于后续的“数学运算”
  • it sounds (to me) like the whole purpose of this exercise is to generate some textual output representing aggregate metadata (of the inputs)听起来(对我来说)这个练习的全部目的是生成一些文本 output 代表(输入的)聚合元数据

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM