Compare output from two commands

Hello folks,
I would like ask for a help in one script I'm currently working on.
Main goal is to compare size of file available on remote site and file already downloaded (check if downloaded file is complete and non corrupted).

# Check if files were downloaded correctly
for FILES in $FILES_TO_DOWN
   do
        LOCAL_SIZE=`ls -la $FILES | awk '{print $5}'`
        REMOTE_SIZE=`$CURL -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES | grep 'Content-Length:' | cut -f 2
 -d" " | tr -d ' '`
        if [ "$LOCAL_SIZE" -eq "$REMOTE_SIZE" ]
                then
                        echo "File is OK"
                else
                        echo "File is corrupted"
        fi
done

In output from script I see this:

+ for FILES in '$FILES_TO_DOWN'
++ ls -la file_20130715.xls
++ awk '{print $5}'
+ LOCAL_SIZE=89984
++ /opt/sfw/bin/curl -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES/file_20130715.xls
++ grep Content-Length:
++ cut -f 2 '-d '
++ tr -d ' '
+ REMOTE_SIZE=$'89984\r'
+ '[' 89984 -eq $'89984\r' ']'
: integer expression expectedine 53: [: 89984
+ echo 'File is corrupted'
File is corrupted

So it looks that local file size is returned as integer and size of remote file is returned as a string.
Can anybody kick me to the right way how do get rid of it?? I suppose that reasonable is try to get size of remote file as well as integer.

Many thanks,
Stan

You need to remove \r
Try this...

REMOTE_SIZE=`$CURL -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES | grep 'Content-Length:' | cut -f 2
 -d" " | tr -d ' ' | sed 's/\r//g' `

--ahamed

---------- Post updated at 03:54 AM ---------- Previous update was at 03:50 AM ----------
Or

REMOTE_SIZE=$( $CURL -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES | 
awk '/Content-Length/{sub(/\r/,"",$2); print $2}' )

--ahamed

---------- Post updated at 04:09 AM ---------- Previous update was at 03:54 AM ----------

I just noticed the $ in the output. Can you paste the output of the curl command?

--ahamed

Yep....

/opt/sfw/bin/curl -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES/file_20130715.xls
HTTP/1.1 200 OK
Date: Mon, 12 Aug 2013 12:00:06 GMT
Content-Type: application/vnd.ms-excel
Last-Modified: Mon, 03 Jun 2013 05:00:30 GMT
Content-Length: 89984
Cache-control: private
Pragma: no-cache
Connection: close
X-CRC: 3A871FE5
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block

What I'm trying to dig out from output is Content-Length as integer, but it seems that I get is as a string.

/opt/sfw/bin/curl -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES/file_20130715.xls| grep 'Content-Length:' | cut -f 2 -d " "
89984

You can use the command which I gave you, that will work!
Try and let us know. In case, if it doesn't, paste the debug output.

--ahamed

First try with awk '{print $2}' where I should get the same result as from previous try

++ ls -la file_20130715.xls
++ awk '{print $5}'
+ LOCAL_SIZE=89984
++ /opt/sfw/bin/curl -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES/file_20130715.xls
++ grep Content-Length:
++ awk '{print $2}'
+ REMOTE_SIZE=$'89984\r'
+ '[' 89984 -eq $'89984\r' ']'
: integer expression expected 29: [: 89984
+ echo 'File is corrupted'
File is corrupted

Next with yours first command

++ ls -la file_20130715.xls
+ LOCAL_SIZE=89984
++ grep Content-Length:
++ /opt/sfw/bin/curl -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES/file_20130715.xls
++ cut -f 2 '-d '
++ tr -d ' '
++ sed 's/\r//g'
+ REMOTE_SIZE=$'89984\r'
+ '[' 89984 -eq $'89984\r' ']'
: integer expression expected 29: [: 89984
+ echo 'File is corrupted'
File is corrupted

Second one from you with awk

++ ls -la file_20130715.xls
++ awk '{print $5}'
+ LOCAL_SIZE=89984
++ /opt/sfw/bin/curl -sI -u user:pass --proxy http://gw:8080 --proxy-user user:pass https://remote_site/$FILES/file_20130715.xls
++ awk '/Content-Length/{sub(/\r/,"",$2); print $2}'
awk: syntax error near line 1
awk: illegal statement near line 1
+ REMOTE_SIZE=
+ '[' 89984 -eq '' ']'
./download1.sh: line 29: [: : integer expression expected
+ echo 'File is corrupted'
File is corrupted

Are you on solaris? Try using nawk

--ahamed

1 Like

Yep this is Solaris, sorry I forgot to mention it on the beginning since I suppose that grep/sed/awk etc. are having the same syntax.
Anyway this solved my problem.

Many thanks for your help Ahamed, You rock!

Stan