批处理文件 - 检查参数

时间:2013-02-06 16:08:29

标签: batch-file

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

然后我有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 a linguagem(J)ava或(C)++:j C:\门廊\门廊-1.0.2 \例子\ 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

2 个答案:

答案 0 :(得分:2)

在您的:checkargs中,%0 将始终包含某些内容 - 这是批处理文件的名称 - 因此无需测试该值

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

:execC

,在第二次测试后你不会跳转到任何地方,所以:execJ也会被执行。

:execJ中的

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

答案 1 :(得分: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.