I was able to reproduce your issue, which for me was indeed caused by a carriage return at the end of the value of $head
. That arises from the HTTP protocol specification: HTTP uses carriage return / newline sequences as line terminators (like DOS and Windows), and awk
will not recognize the carriage return as whitespace.
Your attempt to remove the carriage return ...
head=${head//\r}
... does not serve that purpose. The \
is a general escape character to the shell, preserving the literal meaning of whatever character follows. Since the character r
has no special significance to the shell, the above code is equivalent to
# Oops, not what you meant:
head=${head//r}
I used this alternative:
# Strip all non-digits:
head=${head//[^0-9]}
, and that resolved the issue for me.
Do also note, however, that
- you have misspelled "Length", and
- HTTP header names are not case sensitive, but by default,
awk
regular expressions are. Therefore, your awk
command might in some cases fail to match the wanted header even after the spelling is corrected.
Here's a version of your script that addresses all of these issues:
head=$(curl -sI "$dir" | awk 'BEGIN{IGNORECASE=1} /Content-Length/{print $2}')
head=${head//[^0-9]}
res=$(($head/3))
The last two lines could be merged into one, but I have kept them separate for clarity. That might also serve well if you want to use the value of $head
for other purposes, too.