0

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.

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. 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.

I am not a complete beginner but also not anywhere near advanced linux bash user. Thanks to any help in advance and sorry for bad english!

2
  • While registering on SO, there ought to be a question: How did you come to know about us? Commented Jul 5, 2013 at 11:53
  • grep, sed, awk and maybe cut are commands that are helpful for doing what you want. The man pages should help and a lot of examples for nearly everything are shared across the internet. :) Commented Jun 13, 2016 at 11:14

2 Answers 2

2

An easier way might be to use diff, no?

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)

# 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. 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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Could be simplified using process substitution: diff <(command_A) <(command_B).
0

Use the IFS (internal field separator) to separate on newlines and store the outputs in an array.

#!/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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.