Comparing variable to string in Bash -
i've tried suggestion @ how compare 2 string variables in 'if' statement in bash?
but it's not working me.
i have
if [ "$line" == "http/1.1 405 method not allowed" ] <do whatever need here> else <do else> fi
no matter what, goes else statement. echoing $line ahead of this, , copied , pasted result, sure string right.
any on why happening appreciated.
if read line compliant http network connection, has carriage return character @ end (\x0d
, represented \r
), since http protocol uses cr-lf terminated lines.
so you'll need remove or ignore cr.
here couple of options, if using bash:
remove cr (if present) line, using bash find-and-replace syntax:
if [ "${line//$'\r'/}" = "http/1.1 405 method not allowed" ];
use glob comparison prefix match (requires
[[
instead of[
, better anyway):if [[ "$line" = "http/1.1 405 method not allowed"* ]];
you use regex comparison in
[[
substring match, possibly regular expression:if [[ $line =~ "method not allowed" ]];
(if use regular expression, make sure regular expression operators not quoted.)
Comments
Post a Comment