Page 1 of 1

string not working

Posted: 26 Aug 2019 08:30
by Rajnishjc_27
hi

i am using below script which is not working

set res1='YES'

echo !res1!
if "!res1!" EQU "YES" echo "HI"

its not displaying HI , please help

Re: string not working

Posted: 26 Aug 2019 09:20
by aGerman
You compare "'YES'" with "YES". The first has an additional pair of single quotes as assigned using SET.

Steffen

Re: string not working

Posted: 26 Aug 2019 21:46
by Rajnishjc_27
Hi Steffen

Then how it should be ?

Re: string not working

Posted: 27 Aug 2019 02:55
by ShadowThief
You've got two options:

Option 1 (recommended) - Use double quotes instead of single quotes in the set statement. Note that if you go this route, you should move the first double quote to the left of the variable name so that you get the benefit of quotes without the quotes actually being appended to the value of the string.

Code: Select all

set "res1=YES"

echo !res1!
if "!res1!" EQU "YES" echo "HI"
Option 2 - Include the single quotes in your if statement

Code: Select all

set res1='YES'

echo !res1!
if "!res1!" EQU "'YES'" echo "HI"
Note that in both situations, your code will print "HI" instead of HI because echo does not need quotes.