Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Curl Output compare with a variable?

I have this code fetching file size from curl:

file_size=$(curl -sI -b cookies.txt -H "Authorization: Bearer $access_token" -X GET "https://url.com/api/files$file" | grep Content-Length | awk '{print $2}')

I have another set of variables:

output_filesize_lowerBound=25000000
output_filesize_upperBound=55000000
wrong_filesize=317

I want to write an if statement that compares them and allows me to process it. Sample:

if [[ ( "$file_size" > "$output_filesize_lowerBound" ) && ( "$file_size" < "$output_filesize_upperBound" ) ]]
then
        echo "Writing"
        curl -b cookies.txt -H "Authorization: Bearer $access_token" -X GET "https://url.com/api/files$file" -o "$output_file"
elif [[ ( "$file_size" == "$wrong_filesize" ) ]]
then
        echo "Wrong File"
else
        echo "There is some problem with the file's size, please check it online"
fi

Somehow it's not working for wrong files i.e. it doesn't go to the second if and executes first every time.

I wasted almost an entire day trying out every alternatives I could find.

like image 668
ishallwin Avatar asked Mar 27 '26 21:03

ishallwin


1 Answers

First off, I'd suggest using a different strategy for fetching object size. I've used both of these at various times:

file_size="$(curl -s -o/dev/null -w '%{size_download}' "$url")"

or

file_size="$(curl -sI "$url" | awk '{a[$1]=$2} END {print a["Content-Length:"]}')"

The first one downloads the whole object and returns the number of bytes that curl actually saw. It'll work for URLs that don't return the length header. The second one uses curl -I to download only the headers.

Note that you could also parse curl output in pure bash, without using awk. But whatever, it all works. :)

Second, issue is that your if notation may not work with the results you're getting. You may need to add some debug code to make it more obvious where the problem actually lies. I recommend testing each potential failure separately and reporting on the specific problems:

if (( file_size < output_filesize_lowerBound )); then
    echo "ERROR: $file_size is too small." >&2

elif (( file_size > output_filesize_upperBound )); then
    echo "ERROR: $file_size is too big." >&2

elif (( file_size == wrong_filesize )); then
    # This will never be hit if wrong_filesize is < lowerBound.
    echo "ERROR: $file_size is just plain wrong." >&2

else
    echo "Writing"
    ...

fi

By testing your limits individually and including the tested data in your error output, it'll be more obvious exactly what is causing your script to behave unexpectedly.

For example, if you want the wrong_filesize test to be done BEFORE file_size is tested against lowerBound, you can simply reorder the tests.

like image 72
ghoti Avatar answered Mar 29 '26 11:03

ghoti