console shows "unknown operand" from a if statement

4.8k Views Asked by At

I'm trying to script an automatic md5sum check for my embedded system running uClinux. The script is generated on my computer as well as the tar file I want to check.

The script goes like this :

#!/bin/sh
filename='My_File'
md5='d4deeac6f655ee5d4b9ec150fc6957a5'

if test ! -e $filename.tar
then
    echo Update file does not exist
    exit 1
fi  

if [ -z `md5sum "$filename.tar" | grep $md5` ]
then
    echo 'md5sum is not correct'
    exit 1
else
    echo 'md5sum is correct'
fi  

tar -xvf "$filename.tar"
[...]

The md5sum check run as expected, i-e the script stops when the checksum is wrong and executes to the end otherwise. But when the checksum is correct, I get this message from the console :

[: My_File.tar: unknown operand

I don't understand why I get this, and I think this is not accurate to let my script like this. Can someone explain me what's the shell is doing and how to get rid of this message ?

Thanks

1

There are 1 best solutions below

1
John Kugelman On BEST ANSWER

Quote the output of md5sum so it's not split into multiple words. -z only expects one operand.

if [ -z "`md5sum "$filename.tar" | grep $md5`" ]

While we're here, might as well switch to the nicer $(...) syntax.

if [ -z "$(md5sum "$filename.tar" | grep $md5)" ]