0

我必须创建三个批处理文件。两个是配置环境变量,另一个是运行示例。

然后我有confvarsC.bat(C++环境变量)和confvarsJ.bat(Java环境变量)。然后我创建了执行以下操作的 executePortico.bat:测试 java_home 和 rti_home 并调用环境变量文件。但是我需要如果键入“c”的人应该调用 confvarsC.bat 并在 C++ 中运行示例,如果键入“j”的人应该调用 confvarsJ.bat 并运行 Java 示例。

看到我做了:

@echo off

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

set IMP=portico

rem ################################
rem # check command line arguments #
rem ################################
:checkargs
if "%0" == "" goto usage
if "%1" == "" goto usage


rem #######################
rem # test for JAVA_HOME  #
rem #######################
if "%JAVA_HOME%" == "" goto nojava
goto rtihometest

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

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

if "%0" == "c" goto execC
if "%0" == "j" goto execJ
goto finish

:execC
call C:\Arquivos de programas\Portico\portico-1.0.2\etc\confvarsC.bat
if not "%RTI_HOME%" == "" goto execute

:execJ
call C:\Arquivos de programas\Portico\portico-1.0.2\etc\confvarsJ.bat
if not "%RTI_HOME%" == "" goto execute


############################################
### (target) execute #######################
############################################
:execute
SHIFT
set PATH=%JAVA_HOME%\jre\bin\client;%RTI_HOME%\bin;%PATH%
set RTI_FEDDIR=C:\Arquivos de programas\Portico\portico-1.0.2\examples\cpp\cpp13
set EXEC = "C:\Arquivos de programas\Portico\portico-1.0.2\examples\cpp\cpp13"

if "%0" == "m" goto exec1

:exec1
echo %EXEC%%1%IMP%.exe
pause
%EXEC%%1%IMP%.exe %2
goto finish

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

:err
echo Terminated - Error
goto end

:finish
echo Terminated - Normal

:end

我这样做了,请查看我的代码: 但是我有一个问题,当我尝试运行 Java 示例时,他没有进入 executeJava,他总是进入 executeC。

在我的命令提示符中查看:

C:\Portico\portico-1.0.2\exec>executePortico.bat java-hla13 支持 escolha 语言 (J)ava 或 (C)++: j C:\Portico\portico-1.0.2\examples\cpp \cpp13\java-hla13.exe Pressione qualquer tecla para continuar。. . '"C:\Portico\portico-1.0.2\examples\cpp\cpp13\java-hla13.exe"' 不是内部或外部命令、可运行程序或批处理文件。终止 - 正常

Java 示例位于目录中:C:\Portico\portico-1.0.2\examples\java\hla13

@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%=="C" goto execC
if %language%=="J" goto execJ



rem #######################
rem # test for JAVA_HOME  #
rem #######################
if "%JAVA_HOME%"=="" goto nojava
goto rtihometest

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

goto usage



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


echo EU aqui no executeC

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


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


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


################################################
### (target) execute C++ #######################
################################################
: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


#################################################
### (target) execute Java #######################
#################################################
: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
4

2 回答 2

2

在你的:checkargs, %0中总会有一些东西 - 那是批处理文件的名称 - 所以不需要测试那个值

%1是第一个参数,因此:rtihometest您需要将%1与 c 或 j 进行比较,而不是%0

在 中:execC,您不会在第二次测试后跳转到任何地方,因此:execJ也会被执行。

在 中:execJ,您不会在第二次测试后跳转到任何地方,因此:execute无论测试结果如何都会执行。

于 2013-02-06T18:02:32.950 回答
0

首先,%0 是文件名,%1 是第一个参数。

然后,请:exit在最后加上,当程序需要退出时,输入goto exit.

正确的参数检查批处理文件的示例:

@echo off
set param=%1
set var1=1
set var2=2

if [%1]==[] goto noparam
if %param%==%var1% goto isvar1
if %param%==%var2% goto isvar2
goto unknown

:isvar1
echo The parameter is 1.
goto exit

:isvar2
echo The parameter is 2.
goto exit

:noparam
echo No parameter.
goto exit

:unknown
echo Unknown parameter - %param%.
:exit

以下是各种测试用例(此处文件名为numbers):

C:\Users\user\Documents>numbers
No parameter.

C:\Users\user\Documents>numbers 1
The parameter is 1.

C:\Users\user\Documents>numbers 2
The parameter is 2.

C:\Users\user\Documents>numbers foo
Unknown parameter - foo.
于 2017-09-07T15:02:30.907 回答