0

Doing a basic shell script I'm having trouble with getting the length of a file with curl and later dividing it, and the text:

syntax error: invalid token operator (error token is"

keeps appearing.

Simplified code would be:

    head=$(curl -sI $dir | awk '/Content-Lenth/{print $2}') 
    res=$(($head/3))

I've been reading solutions to similar problems and they said removing the \r that the variable is probably having at its end, so I put this in the middle of the two lines, but the problem is still there:

head=${head//\r}

Any idea why this happens? Is it because of an \r character at the end? If it's the case, how to remove it?

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55

1 Answers1

0

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

  1. you have misspelled "Length", and
  2. 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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • See https://stackoverflow.com/questions/15520339/how-to-remove-carriage-return-from-a-variable-in-shell-script for how to remove CR from a variable. – Barmar Apr 27 '21 at 19:43