LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2016, 11:47 PM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
bash if statement not working


Hello all,

I am trying to test the condition of an if statement against a curl response, however for some reason it will not work no matter how many ways I try, please can someone take a look and see what I could be doing wrong?


Code:
#!/bin/bash

HTTP_200=$(curl -sS -I -L http://127.0.0.1/ | /bin/grep '^HTTP/1.1 200 OK' | awk '{printf "%s", $3}')
echo "op: $HTTP_200"
echo "XXX${HTTP_200}XXX"

if [ "$HTTP_200" = "OK" ]; then
        echo "WORKING"
else
        echo "not working"
fi
output:
Code:
op: OK
XXXOK
not working
 
Old 06-21-2016, 11:54 PM   #2
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
I've noticed this, is there a way to compare against this as a string

Code:
curl -sS -I -L http://127.0.0.1 | /bin/grep '^HTTP/1.1 200 OK' |od -c
0000000   H   T   T   P   /   1   .   1       2   0   0       O   K  \r
0000020  \n
0000021
 
Old 06-22-2016, 12:50 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
The carriage return is what's causing the problem. You can use a search and replace to eliminated it or just use your own output:
Code:
$(curl -sS -I -L http://127.0.0.1/ | awk '/^HTTP\/1.1 200 OK/ { printf "OK" }');
 
1 members found this post helpful.
Old 06-22-2016, 01:04 AM   #4
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Awesome, cheers!
 
Old 06-22-2016, 01:20 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am curious if there is likely to be the word 'OK' anywhere else in the header? If not, you could simplify a little:
Code:
if curl -sS -I -L http://127.0.0.1 | grep -q OK
then
  echo "WORKING"
else
  echo "not working"
fi
 
Old 06-22-2016, 01:30 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by grail View Post
I am curious if there is likely to be the word 'OK' anywhere else in the header?
It's not likely but still possible. One of the other headers could contain the two letters next to each other. The "grep" solution could still look for the right HTTP response status

Code:
if curl -sS -I -L http://127.0.0.1 | grep -qE '^HTTP\/1.1 200 OK'
then
  echo "WORKING"
else
  echo "not working"
fi
The earlier bit about the hidden carriage return brings up an important point that any and all data downloaded (or otherwise inputted) from external sources, especially the net, should not be trusted until it is processed and either replaced or properly cleaned. perl, for example, has a taint mode to detect and tract the spread of such tainted data within a script. Sanitize and validate externally acquired data before using it.
 
Old 06-22-2016, 06:09 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Whole script looks familiar.

I did this 5 years ago:
Code:
###
#!/bin/bash
# Written by user@domain.tld
# Monitors an Apache server and emails the Admin if the
# server reports anything other than "Apache"
# http://bashscripts.org/forum/viewtopic.php?f=16&t=1522
# Last Edited on Thu Dec 29, 2011 - 10:11:40 PM EST
###
MAIL_ME="user@address.com"
MAIL_SUBJECT="Web_Server_needs_attention"
SERVER_DATE=$(TZ=PST8PDT date "+%a %b %d %I:%M:%S %p %Z")
LOCAL_DATE=$(date +%c)

webserver=$(curl -Is http://address.com | \grep -E '^Server' | cut -c9-21)
echo "$SERVER_DATE" - Web Server Check = "$webserver" >> ~/server.log

if [ "$webserver" = "Apache" ] ; then
exit 0
else
 echo "My_Server_Server_Check - FAILED" on "$LOCAL_DATE" | mail "$MAIL_ME" -s "$MAIL_SUBJECT"
 exit 1
fi
#EOF
Hope that helps. May even serve as a bad example. IDK.

Last edited by Habitual; 06-22-2016 at 07:17 AM. Reason: s/"$WEB"/"$webserver"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Conditional statement not working bash script martin083 Programming 2 01-18-2013 06:58 AM
BASH: IF statement not working? EdinburghLad Programming 6 05-15-2012 08:26 PM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration