【问题标题】:Divide an output into multiple variables using shell script使用 shell 脚本将输出划分为多个变量
【发布时间】:2019-04-27 00:26:34
【问题描述】:

所以我有一个输出许多数字的 C 程序。我必须全部检查。问题是,每次我运行我的程序时,我都需要更换种子。为了做到这一点,我一直在手动进行,并试图制作一个 shell 脚本来解决这个问题。

我尝试过使用 sed 但无法做到。

我试图得到这样的输出:

a=$(./algorithm < input.txt)
b=$(./algorithm2 < input.txt)
c=$(./algorithm3 < input.txt)

每个算法程序的输出是这样的:

12 13 315
1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5

所以变量a拥有所有这些输出,而我需要的是

变量a 包含整个字符串 和变量a1 仅包含第三个数字,在本例中为 315。

另一个例子:

2 3 712
1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21 

echo $b 应该给出这个输出:

2 3 712
1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21 

和echo $b1 应该给出这个输出:

712

谢谢!

【问题讨论】:

  • 必须在第一行添加一个 \n 缺失!对不起各位!

标签: bash shell


【解决方案1】:

不完全是您的要求,但一种方法是将算法的结果存储在数组中,然后取消引用感兴趣的项目。你会写这样的东西:

a=( $(./algorithm < input.txt) )  
b=( $(./algorithm2 < input.txt) )
c=( $(./algorithm3 < input.txt) )

请注意包含语句的额外()。现在,a、b 和 c 是数组,您可以访问感兴趣的项目,例如 ${a[0]} 或 $a[1]。

对于您的特定情况,由于您需要第三个元素,因此应该有 index = 2,因此:

a1=${a[2]}
b1=${b[2]}
c1=${c[2]}

【讨论】:

  • 太棒了,要去试试这个atm。很快就会回来!谢谢
  • 嘿萨尔,所以我尝试了你的代码并给了我这个输出:找不到 a1 命令!
  • 哦,删除= 周围的空格。我会修复答案。
【解决方案2】:

由于您使用的是 Bash shell(请参阅您的标签),因此您可以使用 Bash 数组轻松访问输出字符串中的各个字段。比如像这样:

#!/bin/bash

# Your lines to gather the output:
# a=$(./algorithm < input.txt)
# b=$(./algorithm2 < input.txt)
# c=$(./algorithm3 < input.txt)

# Just to use your example output strings:
a="$(printf "12 13 315 \n 1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5")"
b="$(printf "2 3 712 \n 1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21")"

# Put the output in arrays.
a_array=($a)
b_array=($b)

# You can access the array elements individually.
# The array index starts from 0.
# (The names a1 and b1 for the third elements were your choice.)
a1="${a_array[2]}"
b1="${b_array[2]}"

# Print output strings.
# (The newlines in $a and $b are gobbled by echo, since they are not quoted.)
echo "Output a:" $a
echo "Output b:" $b

# Print third elements.
echo "3rd from a: $a1"
echo "3rd from b: $b1"

这个脚本输出

Output a: 12 13 315 1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5
Output b: 2 3 712 1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21
3rd from a: 315
3rd from b: 712

说明:

这里的技巧是 Bash 中的数组常量(文字)具有以下形式

(<space_separated_list_of_elements>)

例如

(1 2 3 4 a b c nearly_any_string 99)

任何分配了这样一个数组的变量,都会自动成为一个数组变量。在上面的脚本中,a_array=($a) 中发生的情况如下:Bash 将 $a 扩展为 &lt;space_separated_list_of_elements&gt; 并再次读取整个表达式,将其解释为数组常量。

此类数组中的单个元素可以像变量一样通过使用形式的表达式来引用

<array_name>[<idx>]

就像一个变量名。其中,&lt;array_name&gt; 是数组的名称,&lt;idx&gt; 是引用单个元素的整数。对于由数组常量表示的数组,索引从零开始连续计数元素。因此,在脚本中,${a_array[2]} 扩展为数组a_array 中的第三个元素。如果数组的元素更少,a_array[2] 将被视为未设置。

可以通过

分别输出数组a_array中的所有元素,对应的索引数组,以及数组中的元素个数
echo "${a_array[@]}"
echo "${!a_array[@]}"
echo "${#a_array[@]}"

这些命令可用于追踪换行符的命运:鉴于上面的脚本,它仍在$a 中,如(观看引号)所示

echo "$a"

产生

12 13 315
 1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5

但是换行符没有进入数组a_array。这是因为 Bash 将其视为分隔数组赋值中第三个和第四个元素的空格的一部分。如果换行符周围没有多余的空格,这同样适用,如下所示:

12 13 315\n1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5

我实际上假设你的 C 程序的输出是以这种形式出现的。

【讨论】:

  • 所以我正在尝试这个:``` a=($(./solveIt
  • @ArmandusFiltch:不完全是。试试a_array=($(./solveIt &lt; entrada.txt)); a1="${a_array[2]}"; echo "a: $a1"。
  • 所以使用 .c 文件的输出是: 37 19 191 \n 7 9 10 1 3 7 10 6 1 8 7 9 并且回显输出是 "a : 21" D:
  • 只是为了确保我指向正确,“\n”不是字符串,在输出中它是一个实际的断线!
  • @ArmandusFiltch:我更新了示例脚本。原则上,字符串中的换行符不应破坏任何内容。请仔细检查您的 Bash 代码是否有额外的括号:$(./solveIt &lt; entrada.txt) 是一个字符串,而 ($(./solveIt &lt; entrada.txt)) 是一个数组!!!
【解决方案3】:

这会将完整的字符串存储在a[0] 中,将各个字段存储在a[1-N] 中:

$ tmp=$(printf '12 13 315\n1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5\n')

$ a=( $(printf '_ %s\n' "$tmp") )

$ a[0]="$tmp"

$ echo "${a[0]}"
12 13 315
1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5

$ echo "${a[3]}"
315

在您的真实代码中,显然将$(printf '12 13 315\n1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5\n') 替换为$(./algorithm &lt; input.txt)。

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2020-08-05
    • 2015-05-30
    • 1970-01-01
    • 2018-11-06
    • 2017-06-26
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多