1

I've been struggling trying to figure out how to compare the output of a command stored in a variable with the content of another variable, also a string, in Shell Script. I know that it looks like a basic RTFM case but I already did and I'm really failing at solving this problem.

So, the code that I have is the following, related to Android (I'm using the ADB tool) and with comments to help understand it:

# Using the ` char to execute the command and return the result as a string
# To store it in the variable
RES=`adb shell dumpsys power | grep "Display Power"`

# Store the string in the variable
EXPECTED='Display Power: state=OFF'

#Simple checks, both returning "Display Power: state=OFF" (without quotes) in the console
echo "$RES"
echo "$EXPECTED"


# Compare if the values of the variables, both strings, are equal
# If so, perform action
if [ "$EXPECTED" = "$RES" ]
    then
        echo "inside"
        adb shell input keyevent 26
    fi

The case is that the strings in the IF comparison never seem to be equal.

I think that the mistake is in the first value assignation to the variable RES, because maybe I didn't understood correctly what the ` character means and what does it return is not what it seems to be.

I'm sure you guys can help me here with this basic case.

Thank you very much for your help

2
  • Check the exact contents of the saved stdout. Is there, for example, a trailing newline that is not (yet) in your EXPECTED string? Commented Mar 4, 2016 at 13:05
  • Can you show us the results of the two echo commands in your "Simple Check"? Even better... echo ">${RES}<" and echo ">${EXPECTED}<" Commented Mar 4, 2016 at 13:20

1 Answer 1

1

Your string comparsion seems to be ok, it should work. Probably the problem is that the strings are actually different. You can check detailed differences (i.e. in whitespace or in some sort of control characters like extra tabs or whatever) by using something like:

echo -n "$RES" | hd
echo -n "$EXPECTED" | hd

This will give you the following for $EXPECTED:

00000000  44 69 73 70 6c 61 79 20  50 6f 77 65 72 3a 20 73  |Display Power: s|
00000010  74 61 74 65 3d 4f 46 46                           |tate=OFF|

Compare it throughly with hex dump for $RES.

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

1 Comment

Doing it like this I get the following results: For the value extracted with the grep: 0000000 44 69 73 70 6c 61 79 20 50 6f 77 65 72 3a 20 73 0000010 74 61 74 65 3d 4f 46 46 0d 0a 000001a For the string introduced manually: 0000000 44 69 73 70 6c 61 79 20 50 6f 77 65 72 3a 20 73 0000010 74 61 74 65 3d 4f 46 46 0a 0000019 We can see that we have an extra carriage return and also the last character is different in both outputs. So, the string comparison works but the actual characters in the strings are different. I'll investigate further. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.