简体   繁体   English

从bash命令输出的行作为字符串存储在变量中

[英]Line from bash command output stored in variable as string

I'm trying to find a solution to a problem analog to this one: 我正试图找到一个类似于这个问题的解决方案:

#command_A

A_output_Line_1
A_output_Line_2
A_output_Line_3

#command_B

B_output_Line_1
B_output_Line_2

Now I need to compare A_output_Line_2 and B_output_Line_1 and echo "Correct" if they are equal and "Not Correct" otherwise. 现在我需要比较A_output_Line_2B_output_Line_1并且如果它们相等则回显“正确”,否则比较“不正确”。

I guess the easiest way to do this is to copy a line of output in some variable and then after executing the two commands, simply compare the variables and echo something. 我想最简单的方法是在一些变量中复制一行输出,然后在执行这两个命令之后,只需比较变量并回显一些东西。 This I need to implement in a bash script and any information on how to get certain line of output stored in a variable would help me put the pieces together. 我需要在一个bash脚本中实现,任何有关如何获取存储在变量中的输出行的信息都可以帮助我将这些部分组合在一起。 Also, it would be cool if anyone can tell me not only how to copy/store a line, but probably just a word or sequence like : line 1, bytes 4-12, stored like string in a variable. 此外,如果任何人不仅可以告诉我如何复制/存储一行,而且可能只是一个单词或序列,如:第1行,字节4-12,在变量中存储为字符串,那将会很酷。

I am not a complete beginner but also not anywhere near advanced linux bash user. 我不是一个完整的初学者,但也不是高级linux bash用户附近的任何地方。 Thanks to any help in advance and sorry for bad english! 感谢任何提前帮助,抱歉英语不好!

An easier way might be to use diff , no? 一种更简单的方法可能是使用diff ,不是吗?

Something like: 就像是:

command_A > command_A.output
command_B > command_B.output
diff command_A.output command_B.output

This will work for comparing multiple strings. 这将用于比较多个字符串。

But, since you want to know about single lines (and words in the lines) here are some pointers: 但是,既然你想了解单行(以及行中的单词),这里有一些指示:

# first line of output of command_A
command_A | head -n 1

The -n 1 option says only to use the first line (default is 10 I think) -n 1选项表示只使用第一行(我认为默认为10)

# second line of output of command_A
command_A | head -n 2 | tail -n 1

that will take the first two lines of the output of command_A and then the last of those two lines. 这将占用command_A输出的前两行,然后是这两行中的最后一行。 Happy times :) 欢乐时光 :)

You can now store this information in a variable: 您现在可以将此信息存储在变量中:

export output_A=`command_A | head -n 2 | tail -n 1`
export output_B=`command_B | head -n 1`

And then compare it: 然后比较一下:

if [ "$output_A" == "$output_B" ]; then echo 'Correct'; else echo 'Not Correct'; fi

To just get parts of a string, try looking into cut or (for more powerful stuff) sed and awk . 要获得字符串的一部分,请尝试查看cut或(对于更强大的东西) sedawk

Also, just learing a good general purpose scripting language like python or ruby (even perl ) can go a long way with this kind of problem. 另外,只需学习一种通用的脚本语言如pythonruby (甚至是perl )就可以解决这类问题。

Use the IFS (internal field separator) to separate on newlines and store the outputs in an array. 使用IFS(内部字段分隔符)分隔换行符并将输出存储在数组中。

#!/bin/bash

IFS='
'
array_a=( $(./a.sh) )
array_b=( $(./b.sh) )

if [ "${array_a[1]}" = "${array_b[0]}" ]; then
    echo "CORRECT"
else
    echo "INCORRECT"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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