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:

  1. remove cr (if present) line, using bash find-and-replace syntax:

    if [ "${line//$'\r'/}" = "http/1.1 405 method not allowed" ]; 
  2. use glob comparison prefix match (requires [[ instead of [, better anyway):

    if [[ "$line" = "http/1.1 405 method not allowed"* ]]; 
  3. 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

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -