Bash comparing values

Bash comparing values



I'm getting the size of a file from a remote webserver and saving the results to a var called remote I get this using:


remote


remote=`curl -sI $FILE | grep -i Length | awk '/Content/print $(NF-0)'`



Once I've downloaded the file I'm getting the local files size with:


local=`stat --print="%s" $file`



If I echo remote and local they contain the same value.



I'm trying to run an if statement for this


if [ "$local" -ne "$remote" ]; then



But it always shows the error message, and never advises they match.



Can someone advise what I'm doing wrong.



Thanks






What is output of declare -p local remote

– anubhava
Sep 18 '18 at 17:42


declare -p local remote






This may not be the source of your problem, but local is a reserved word for defining local variables within a function.

– Jason
Sep 18 '18 at 17:43


local






You might have a carriage return at the end of your curl output. Check with printf '%qn' "$remote" if there's a r.

– Benjamin W.
Sep 18 '18 at 18:02


curl


printf '%qn' "$remote"


r




1 Answer
1



curl's output uses the network format for text, meaning that lines are terminated by a carriage return followed by linefeed; unix tools (like the shell) expect lines to end with just linefeed, so they treat the CR as part of the content of the line, and often get confused. In this case, what's happening is that the remote variable is getting the content length and a CR, which isn't valid in a numeric expression, hence errors. There are many ways to strip the CR, but in this case it's probably easiest to have awk do it along with the field extraction:


curl


remote


awk


remote=$(curl -sI "$remotefile" | grep -i Length | awk '/Content/sub("r","",$NF); print $NF')



BTW, I also took the liberty of replacing backticks with $( ) -- this is easier to read, and doesn't have some oddities with escapes that backticks have, so it's the preferred syntax for capturing command output. Oh, and (NF-0) is equivalent to just NF, so I simplified that. As @Jason pointed out in a comment, it's safest to use lower- or mixed-case for variable names, and put double-quotes around references to them, so I did that by changing $FILE to "$remotefile". You should do the same with the local filename variable.


$( )


(NF-0)


NF


$FILE


"$remotefile"



You could also drop the grep command and have awk search for /^Content-Length:/ to simplify it even further.


grep


awk


/^Content-Length:/






Perfect thanks this has helped a lot and has resolved my issue.

– Tom
Sep 19 '18 at 7:17



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ