Answer the question
In order to leave comments, you need to log in
Why does the comparison always return true when comparing two strings in bash?
Friends, hello everyone!
I’ll write right away that I’m not strong in writing bash scripts, but such a need arose.
I am attaching a simple example:
# !/bin/sh
res=$(curl -Isk https://........./api/rest.php | head -n 1 | sed 's/\r//g')
res_string="HTTP/1.1 200 OK"
if [ "$res"="$res_string" ]
then
service nginx status && service httpd status
else
echo "CANSEL"
fi
bash -x script.sh
++ curl -Isk https://...../api/rest.php
++ head -n 1
++ sed 's/\r//g'
+ res='HTTP/1.1 200 OK'
+ res_string='HTTP/1.1 200 OK'
+ '[' 'HTTP/1.1 200 OK=HTTP/1.1 200 OK' ']'
+ service nginx status
nginx (pid 17277) is running...
+ service httpd status
httpd (pid 17321) is running...
bash -x script.sh
++ curl -Isk https://......./api/rest.php
++ head -n 1
++ sed 's/\r//g'
+ res='HTTP/1.1 200 OK'
+ res_string='38942983478239423олвыалодлаывдHTTP/1.1 200 OK'
+ '[' 'HTTP/1.1 200 OK=38942983478239423олвыалодлаывдHTTP/1.1 200 OK' ']'
+ service nginx status
nginx (pid 17277) is running...
+ service httpd status
httpd (pid 17321) is running...
Answer the question
In order to leave comments, you need to log in
because there [ "$res" = "$res_string" ]
are spaces between the space
The space is not put only when assigning a value to a variable
For example,res=123
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question