1

I'm trying to save value from a curl command to a variable in a bash script.

Script looks like this

#!/bin/bash

curr=$(pwd)
IP_addr="192.168.0.102"
username="root"
password="pass"

HTTP_STATUS=$(curl -IL --silent $username:$password@$IP_addr | grep HTTP)
echo
echo "echo the variable works!"
echo $HTTP_STATUS
isOK=$(echo $HTTP_STATUS)
status="HTTP/1.1 401 Unauthorized"

echo

if [[ $HTTP_STATUS == $status ]]; then
    echo "The same the same!"
else
    echo "$isOK is not the same as $status"
fi

echo

if [ "$status" == "$isOK" ]
then
    echo "The same the same!"
else
    echo "$isOK is not the same as $status"
fi

I am deliberately passing the wrong password in order for curl to return HTTP/1.1 401 Unauthorized. I want a function to check if wrong credentials are sent to a server.

The strange thing is that when I save the output from the curl command ie

HTTP_STATUS=$(curl -IL --silent $username:$password@$IP_addr | grep HTTP | tee $curr/test.txt)

To a file with tee I get this printed in file HTTP/1.1 401 Unauthorized. But if I remove the tee command ie

HTTP_STATUS=$(curl -IL --silent $username:$password@$IP_addr | grep HTTP)

And execute the script I get following print in terminal

./test.sh 
echo the variable works!
HTTP/1.1 401 Unauthorized

is not the same as HTTP/1.1 401 Unauthorized

is not the same as HTTP/1.1 401 Unauthorized

I tried with following as well but same result

HTTP_STATUS=`curl -IL --silent $username:$password@$IP_addr | grep HTTP`

The variable HTTP_STATUS seems to be blank when I do the checks in the if statements. How is this possible and why is the output of the command saved to a file with tee and echo the variable works but not when using the variable in the if statements?

Best regards

3
  • Always quote your parameter expansions until you learn when it is safe or appropriate to leave them unquoted. – chepner Dec 17 '16 at 17:34
  • I found a answer here stackoverflow.com/questions/12444125/…. For some reason. When using stars ** around "$status" such as "$status" it works. But I still don't know why this is! – S4M1R Dec 17 '16 at 17:55
  • The stars create a pattern, which matches any string that starts with a (possibly empty) sequence of characters, then $status, then ending with a (possibly empty) sequence of characters. – chepner Dec 17 '16 at 18:00
2

The HTTP protocol requires that header lines end with <CR><LF> (carriage return and line feed, \r\n in UNIX notation). To see what curl actually returns you can try:

curl -IL --silent $username:$password@$IP_addr | grep HTTP | cat -v

In UNIX, <LF> terminates the text line, and <CR> is just an ordinary character with no special meaning. The apparently missing $isOK in the subsequent messages is due to the trailing <CR>, which moves the cursor back at the beginning of the line. In detail, the line

echo "$isOK is not the same as $status"

writes out

HTTP/1.1 401 Unauthorized<CR>
 is not the same as HTTP/1.1 401 Unauthorized

both on the same line.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.