简体   繁体   中英

Bash Script Comparing File Sizes Issue

I'm running OS X.

So I'm having problems with a script to compare a file's size on my local HD, and a server. To do so, I use cURL to get the http header, and trim it to the size in KB. Then I use "stat" to get the local file's size.

Here's my code:

clear
cd "$(dirname "$0")"

Local=$(stat -f "%z" ./Google.png)
Remote=$(curl -sI http://www.google.com/intl/en_com/images/srpr/logo3w.png | grep Content-Length | awk '{print $2}')
declare -i Local
declare -i Remote
echo $Local
echo $Remote

if [ $Local != $Remote ]; then
  echo "Different sizes."
else
  echo "Same size."
fi

No matter if the sizes are equal or not, I get:

7007
7007
Different sizes.

I'm really desperate on this one, can anyone help?

使用[ $n -eq $k ]比较数字。

curl's output has lines terminated with carriage-return+linefeed (\\r\\n), not just linefeed (\\n); the carriage return is getting included in the value of Remote, and causing the confusion. You could remove it by piping through tr -d "\\r" , but it's probably simpler to have awk do it (and the search, for that matter):

Remote=$(curl -sI http://www.google.com/intl/en_com/images/srpr/logo3w.png | awk '/Content-Length/ {sub("\r",""); print $2}')

BTW, I don't think that the declare -i commands are doing anything useful (since Local and Remote have already been set).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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