批处理 - IF语句

时间:2013-02-20 12:33:25

标签: batch-file

如果键入“c”的人应该在C ++中运行示例,我需要这样做, 如果人类“j”应该运行Java示例。

可是:

1-当我输入“c”或“j”时无法识别,如果输入“j”,他会执行executeC, 如果我选择“c”也会使executeC没有进入这个executeJava。然后改为数值,如果我输入3 - executeC,如果我输入4 - executeJava。 因此,如果我键入3,它将访问目录C:\ Portico \ portico-1.0.2 \ examples \ cpp \ cpp13并处理executeC, 2-如果我输入4,它将访问目录C:\ Portico \ portico-1.0.2 \ examples \ java \ hla13,但不运行,只是结束。

查看我在命令提示符中执行的操作

C:\Portico\portico-1.0.2\exec>executePortico.bat main
Choose (J)ava or (C)++: 3
C:\Portico\portico-1.0.2\examples\cpp\cpp13\main.exe
Pressione qualquer tecla para continuar. . .
Created Federation
Joined Federation as exampleFederate
Successfully registered sync point: ReadyToRun

C:\Portico\portico-1.0.2\exec>executePortico.bat java-hla13
Choose (J)ava or (C)++: 4
C:\Portico\portico-1.0.2\examples\java\hla13\java-hla13.jar
Pressione qualquer tecla para continuar. . .
Terminated - Normal
C:\Portico\portico-1.0.2\exec> 

请查看我的代码:

@echo off

rem ###########################
rem # implementation/version  #
rem ###########################


rem ################################
rem # check command line arguments #
rem ################################
:checkargs


if "%0"=="" goto usage
if "%1"=="" goto usage

set /p language=Por favor escolha a linguagem (J)ava or (C)++: 

if %language%==3 goto execC
if %language%==4 goto execJ



rem #######################
rem # test for JAVA_HOME  #
rem #######################
set JAVA=java
set JAVAC=javac
set JAR=jar
if "%JAVA_HOME%"=="" goto nojava

set JAVA="%JAVA_HOME%\bin\java"
set JAVAC="%JAVA_HOME%\bin\javac"
set JAR="%JAVA_HOME%\bin\jar"
goto rtihometest

:nojava
echo ERROR Your JAVA_HOME environment variable is not set!

goto usage


rem #######################
rem # test for RTI_HOME   #
rem #######################
:rtihometest


:execC
call C:\Portico\portico-1.0.2\etc\confvarsC.bat
if "%RTI_HOME%"=="" goto nortihome 
if not "%RTI_HOME%"=="" goto executeC

goto End


:execJ
call C:\Portico\portico-1.0.2\etc\confvarsJ.bat
if "%RTI_HOME%"=="" goto nortihome 
if not "%RTI_HOME%"=="" goto executeJava

goto End


:nortihome
echo ERROR Your JAVA_HOME environment variable is not set!
goto usage


############################################
### (target) execute #######################
############################################
:executeC

set PATH=%JAVA_HOME%\jre\bin\client;%RTI_HOME%\bin;%PATH%
set RTI_FEDDIR=C:\Portico\portico-1.0.2\examples\cpp\cpp13
set EXEC=C:\Portico\portico-1.0.2\examples\cpp\cpp13\

echo %EXEC%%1.exe
pause
"%EXEC%%1.exe"

goto finish


:executeJava


set PATH=PATH=%RTI_HOME%\include\ng6;%RTI_HOME%\lib;%RTI_HOME%\bin;%JAVA_HOME%\bin;%PATH%
set ClassPath=%RTI_HOME%\lib\portico.jar
set RTI_FEDDIR=C:\Portico\portico-1.0.2\examples\java\hla13
set EXECUTA=C:\Portico\portico-1.0.2\examples\java\hla13\

echo %EXECUTA%%java-hla13.jar
pause
"%EXECUTA%%java-hla13.jar"

goto finish



:usage


echo usage: executePortico.bat [model] [nome modelo]
goto err

:err
echo Terminated - Error
goto end

:finish
echo Terminated - Normal

:end

1 个答案:

答案 0 :(得分:0)

您似乎想要输入JC进行分支,但是您正在测试3和4.它们都会失败并且代码会快速继续,因为没有其他逻辑处理错误的输入。试试这个细分:

if "%~0"=="" goto usage
if "%~1"=="" goto usage

set /p "language=Por favor escolha a linguagem (J)ava or (C)++: "

if /i "%language%"=="C" goto execC
if /i "%language%"=="J" goto execJ
echo incorrect input - quitting
goto :EOF