What is the syntax for .bat?

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

This basically means the following:

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

?

What setlocalfor?

How does bat identify endif?

+3
source share
3 answers

to try

setlocal /? 

on the command line

As for the operators if: help ifshould show you everything you need to know. Batch files have only one line ifform expressions

if[state] [operator]
if[state] [operator] else[operator]

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

if[status] (
[Approval]
[Approval]
...
)

if [] (
[]
...
) else (
[]
...
)

end if. if .

, . help set .

+3

: Apache Tomcat

, , . .

  • "%OS%" == "Windows_NT"

    Tomcat (catalina.bat) :

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

    Tomcat 8 ( Tomcat 8 Tomcat 9 - ), :

    setlocal
    

    %OS% Windows_NT Windows. , Microsoft Windows if . setlocal.

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

      % OS% , . Windows NT, Windows 2000, Windows XP Windows 7 "Windows_NT" . % OS%, DOS- Digital Research-origin, DOS, DOS, REAL/32, DOS Plus, DR DOS, Novell DOS OpenDOS.

    • Windows 8 Windows 10, ( "Windows_NT" ).

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

      , DOS, , [5]. "DOSPLUS" (DOS Plus 1.2 DOS), "CPCDOS 4.1" (DOS Plus 1.2 CP/M), "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" "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" ( DOS), "CPCDOS" ( DOS), "CDOS386" ( DOS 386), "DRMDOS" (DR Multiuser DOS), "MDOS" ( DOS CCI), "REAL32" (REAL/32). [2] [6] MS-DOS INTERSVR "DRDOS". [6] . % OS%, Microsoft Windows NT.

  • if

    Tomcat ( 8), "%OS%" == "Windows_NT" if "%OS%" == "Windows_NT" setlocal , setlocal . .

    , (, , - . ), ( endif - @setlocal).

    :

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

    testif.bat :

    b
    c
    d
    

    , echo a, . , endif echo a ( echo b).

  • setlocal

    Tomcat (\ bin\startup.bat) CATALINA_HOME CATALINA_BASE :

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

    setlocal. setlocal, script. " ", script, ( ), , .

    :

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

    script (1.bat) :

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

    , A C, setlocal endlocal. , echo , ( %A%, %B%, %C%).

    endlocal, , :

    :

0
source

Source: https://habr.com/ru/post/1734233/


All Articles