user198729
user198729

Reputation: 63676

What's the syntax for .bat?

if "%OS%"=="Windows_NT" @setlocal
...
if "%OS%"=="Windows_NT" @endlocal

Does the above basically mean this:

if(OS == 'Windows_NT'):
...
endif

?

What's setlocal for ?

How do bat identify the endif?

Upvotes: 3

Views: 1921

Answers (3)

ROMANIA_engineer
ROMANIA_engineer

Reputation: 56694

Explanation based on a practical example: Apache Tomcat

I chose this example because it has everything that you want to know. I also added some simple examples.

  1. "%OS%" == "Windows_NT"

    It was used in old versions of Tomcat (catalina.bat) as:

    if "%OS%" == "Windows_NT" setlocal
    

    but starting from Tomcat 8 (at this moment only Tomcat 8 and Tomcat 9 - milestone version are available) it was replaced by:

    setlocal
    

    because %OS% is always Windows_NT on the new Windows operating system. So, on the modern Microsoft Windows versions, that if is useless. Just use setlocal.

    References:

    • https://en.wikipedia.org/wiki/Environment_variable#OS (Windows) :

      The %OS% variable contains a symbolic name of the operating system family to distinguish between differing feature sets in batchjobs. Under Windows NT, Windows 2000, Windows XP and Windows 7, it always holds the string "Windows_NT". It resembles an identically named environment variable %OS% found in all DOS-related operating systems of Digital Research-origin like Concurrent DOS, Multiuser DOS, REAL/32, DOS Plus, DR DOS, Novell DOS and OpenDOS.

    • I didn't find any reference for Windows 8 and Windows 10, but I tested it on both of them and it has the same behavior (it contains "Windows_NT").

    • https://en.wikipedia.org/wiki/Environment_variable#.25OS.25 (DOS) :

      This variable contains the name of the operating system in order to distinguish between different DOS-related operating systems of Digital Research-origin in batch jobs and applications.[5] Known values include "DOSPLUS" (DOS Plus 1.2 in DOS emulation), "CPCDOS 4.1" (DOS Plus 1.2 in CP/M emulation), "DRDOS" (DR DOS 3.31-6.0, DR DOS Panther, DR DOS StarTrek, DR-DOS 7.02[5]-7.05), "EZDOS" (EZ-DOS 3.41), "PALMDOS" and "NetWare PalmDOS" (PalmDOS 1.0), "NWDOS" (Novell DOS 7), "NWDOS7" (Novell DOS 7 Beta), "OPENDOS" (Caldera OpenDOS 7.01, Caldera DR-OpenDOS 7.02), "CDOS" (Concurrent DOS), "CPCDOS" (Concurrent PC DOS), "CDOS386" (Concurrent DOS 386), "DRMDOS" (DR Multiuser DOS), "MDOS" (CCI Multiuser DOS), "REAL32" (REAL/32).[2][6] MS-DOS INTERSVR looks for a value of "DRDOS" as well.[6] See also the identically named environment variable %OS% later introduced in the Microsoft Windows NT family.

  2. if

    In the old Tomcat versions (before 8), if the condition "%OS%" == "Windows_NT" in if "%OS%" == "Windows_NT" setlocal was false, only setlocal would have been skipped. The other commands would have been run normally.

    In your case, if the condition is true (and it probably is - see the explanation above), then only one command will be run (that virtual endif comes exactly at the end of the row - just after @setlocal).

    The following simple example should be clear:

    @echo off
    rem testif.bat
    if "1" == "2" echo a
    echo b
    
    if "1" == "1" echo c
    echo d
    

    Running testif.bat from a console prints:

    b
    c
    d
    

    Even if the first condition was false, only echo a was influenced by this value because it was skipped. So, the virtual endif comes after echo a (before echo b).

  3. setlocal

    In Tomcat (\bin\startup.bat), you can set the CATALINA_HOME and CATALINA_BASE variables like this:

    set CATALINA_HOME=%~dp0
    set CATALINA_BASE=%CATALINA_HOME%
    

    after the setlocal line. If you add those lines before setlocal, those two variables will be available after the end of the batch script. They don't have "side-effects" if you run the script using double click (the normal way), but it matters if you want to run it from a command prompt.

    A simplified version:

    @echo off
    rem 1.bat
    set A=1
    setlocal
    set B=2
    endlocal
    set C=3
    

    Run the script above (1.bat) from a command prompt and after that run, run the following commands:

    rem - prints 1
    echo %A%
    rem - prints %B%
    echo %B%
    rem - prints 3
    echo %C%
    

    So, because you don't want to have values for A and C, you should set the environment variable between setlocal and endlocal. Of course, if you run the same echo commands from a new prompt, none of them will be set (the output will be %A%, %B%, %C%).

    If there is no explicit endlocal, imagine that it's at the end of the batch file:

    Reference:

    • https://technet.microsoft.com/en-us/library/bb491001.aspx

      Use setlocal to change environment variables when you run a batch file. Environment changes made after you run setlocal are local to the batch file. Cmd.exe restores previous settings when it either encounters an endlocal command or reaches the end of the batch file.

Upvotes: 0

YOU
YOU

Reputation: 123871

try

setlocal /? 

on command prompt

As for if statements: help if should show you everything you need to know. Batch files have only single line if statements of the forms

if    [condition]    [statement]
if    [condition]    [statement]    else    [statement]

However, [statement] can be a block, delimited by parentheses:

if    [condition]    (
    [statement]
    [statement]
    ...
)

if    [condition]    (
    [statement]
    ...
)    else    (
    [statement]
    ...
)

There is no explicit end if keyword. The end of the if statement is marked by the end of the line or by the end of the parenthesized block.

Also keep in mind that you need to be careful with setting and subsequently using environment variables in a single block. Read up in help set on delayed expansion for the pitfalls there.

Upvotes: 3

Related Questions