Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dos batch - basic if/else not working

Tags:

batch-file

dos

I have:

@echo off
echo before
IF 1 == 1 (
    echo got it
) ELSE (
    echo missed
}
echo done

This code just prints "before" and nothing else. I have no idea what I'm missing...

like image 538
Stephane Grenier Avatar asked Mar 02 '13 07:03

Stephane Grenier


Video Answer


2 Answers

You've got the wrong kind of bracket at the end of your ELSE - you've got } instead of ). Changing it to:

@echo off
echo before
IF 1 == 1 (
    echo got it
) ELSE (
    echo missed
)
echo done

the output is:

before
got it
done
like image 119
Jon Skeet Avatar answered Nov 15 '22 07:11

Jon Skeet


this works just like if else in nested style

IF %CHUSEL%==0 (
  SET IP=10.148.24.1
) ELSE (
  IF %CHUSEL%==1 (
      SET IP=10.148.24.2
  ) ELSE (
      IF %CHUSEL%==2 (
          SET IP=10.148.24.3
      ) ELSE (
          IF %CHUSEL%==3 (
             SET IP=10.148.24.4
          )
      )
   )
)
like image 45
sanu Avatar answered Nov 15 '22 06:11

sanu