批处理文件 - 检查参数 [英] Batch files - checking arguments
问题描述
我要创建三个批处理文件。其中2是配置环境变量,另将运行一个例子。
然后,我有confvarsC.bat(C + +环境变量)和confvarsJ.bat(Java环境变量)。
然后,我创建了执行以下操作的executePortico.bat:测试JAVA_HOME的rti_home并调用环境变量的文件。
但我需要,如果人键入C应该叫confvarsC.bat和运行C的例子+ +,如果人型J应该叫confvarsJ.bat和运行Java的例子。
看到我所做的:
关闭@echoREM ###########################
REM#实现/版本#
REM ###########################设置IMP =门廊REM ################################
REM#检查命令行参数#
REM ################################
:checkargs
如果0%==goto语句用法
如果%1==goto语句用法
REM #######################
对于JAVA_HOME#REM#测试
REM #######################
如果%JAVA_HOME%==转到nojava
转到rtihometest:nojava
回声错误您的JAVA_HOME环境变量没有设置!
转到用法REM #######################
对于RTI_HOME#REM#测试
REM #######################
:rtihometest如果0%==C转到execC
如果0%==J转到execJ
转到结束:execC
调用C:\\ Arquivos德计划成\\门廊\\门廊-1.0.2 \\等\\ confvarsC.bat
如果不是%RTI_HOME%==goto语句执行:execJ
调用C:\\ Arquivos德计划成\\门廊\\门廊-1.0.2 \\等\\ confvarsJ.bat
如果不是%RTI_HOME%==goto语句执行
############################################
###(目标)执行#######################
############################################
:执行
转移
设置PATH =%JAVA_HOME%\\ jre的\\ BIN \\客户端;%RTI_HOME%\\ BIN;%PATH%
设置RTI_FEDDIR = C:\\ Arquivos德计划成\\门廊\\门廊-1.0.2 \\例子\\ CPP \\ cpp13
设置EXEC =C:\\ Arquivos德计划成\\门廊\\门廊-1.0.2 \\例子\\ CPP \\ cpp13如果0%==M转到exec1:exec1
回声%EXEC %% 1%IMP%的.exe
暂停
%EXEC %% 1%IMP%的.exe 2%
转到结束:用法
回声用法:executePortico.bat [产品型号] [诺姆莫德洛]
转到犯错:呃
回声终止 - 错误
转到结束:完
呼应终止 - 正常:结束
我这样做,看到我的code请:
但我有一个问题,当我尝试运行Java例子,他不进入executeJava,他总是进入executeC。
请参阅我在条命令提示:
C:\\门廊\\门廊-1.0.2 \\ EXEC> executePortico.bat Java的hla13
POR青睐escolha一个linguagem(J)AVA或(C)++:J-
C:\\门廊\\门廊-1.0.2 \\例子\\ CPP \\ cpp13 \\ java的hla13.exe
pressione qualquer tecla第continuar。 。 。
'C:\\门廊\\门廊-1.0.2 \\例子\\ CPP \\ cpp13 \\ java的hla13.exe不被识别为一个内部或外部命令,可操作的程序或批处理文件。
终止 - 正常
和Java的例子是,在目录:C:\\门廊\\门廊-1.0.2 \\例子\\ java的\\ hla13
关闭@echoREM ###########################
REM#实现/版本#
REM ###########################
REM ################################
REM#检查命令行参数#
REM ################################
:checkargs
如果0%==goto语句用法
如果%1==goto语句用法集/ p LANGUAGE = POR青睐escolha一个linguagem(J)AVA或(C)++:如果语言%==%C转到execC
如果语言%==%J转到execJREM #######################
对于JAVA_HOME#REM#测试
REM #######################
如果%JAVA_HOME%==转到nojava
转到rtihometest:nojava
回声错误您的JAVA_HOME环境变量没有设置!转到用法REM #######################
对于RTI_HOME#REM#测试
REM #######################
:rtihometest
回声欧盟褐没有executeC:execC
调用C:\\门廊\\门廊-1.0.2 \\等\\ confvarsC.bat
如果%RTI_HOME%==转到nortihome
如果不是%RTI_HOME%==转到executeC
:execJ
调用C:\\门廊\\门廊-1.0.2 \\等\\ confvarsJ.bat
如果%RTI_HOME%==转到nortihome
如果不是%RTI_HOME%==转到executeJava
:nortihome
回声错误您的JAVA_HOME环境变量没有设置!
转到用法
################################################
###(目标)执行C ++ #######################
################################################
:executeC设置PATH =%JAVA_HOME%\\ jre的\\ BIN \\客户端;%RTI_HOME%\\ BIN;%PATH%
设置RTI_FEDDIR = C:\\门廊\\门廊-1.0.2 \\例子\\ CPP \\ cpp13
设置EXEC = C:\\门廊\\门廊-1.0.2 \\例子\\ CPP \\ cpp13 \\回声%EXEC %%的1.exe
暂停
%EXEC %%的1.exe转到结束
#################################################
###(目标)执行Java #######################
#################################################
:executeJava设置PATH = PATH =%RTI_HOME%\\包括\\ NG6;%RTI_HOME%\\ lib目录;%RTI_HOME%\\ BIN;%JAVA_HOME%\\ BIN;%PATH%
设置CLASSPATH =%RTI_HOME%\\ LIB \\ portico.jar
设置RTI_FEDDIR = C:\\门廊\\门廊-1.0.2 \\例子\\ java的\\ hla13
设置EXECUTA = C:\\门廊\\门廊-1.0.2 \\例子\\ java的\\ hla13 \\
回声%EXECUTA%java的hla13.jar
暂停
%EXECUTA%java的hla13.jar转到结束:用法
回声用法:executePortico.bat [产品型号] [诺姆莫德洛]
转到犯错:呃
回声终止 - 错误
转到结束:完
呼应终止 - 正常:结束
在:checkargs
,%0 将永远在东西 - 即批处理文件的名称 - 因此没有必要测试值
%1 是第一个参数,所以:rtihometest
你需要比较的%1 C或Ĵ,不是%0
在:execC
,你没有任何地方的第二次测试后跳,所以:execJ
也将被执行
在:execJ
,你没有任何地方的第二次测试后跳,所以:执行
将照常执行的测试结果。
I had to create three batch files. two would be to configure the environment variables and the other would run an example.
Then I have confvarsC.bat (C + + environment variables)and confvarsJ.bat (Java environment variables). And then I created the executePortico.bat that does the following: tests the java_home the rti_home and calls the file of environment variables. But I need that if the person typing "c" should call the confvarsC.bat and run the examples in C + +, if the person type "j" should call the confvarsJ.bat and run the Java examples.
See that i did:
@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
I did this, see my code please: But i have a problem, when I try to run a Java example, he does not enter the executeJava, he always goes into executeC.
See in my commmand prompt:
C:\Portico\portico-1.0.2\exec>executePortico.bat java-hla13 Por favor escolha a linguagem (J)ava or (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"' is not recognized as an internal or external command, operable program or batch file. Terminated - Normal
And the Java examples are in directory: 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
in your :checkargs
, %0 will always have something in - that is the name of the batch file - so there is no need to test that value
%1 is the first parameter, so in :rtihometest
you need to compare %1 with c or j, not %0
in :execC
, you do not jump anywhere after the second test, so :execJ
will be executed too.
in :execJ
, you do not jump anywhere after the second test, so :execute
will be executed regardless of the result of the test.
这篇关于批处理文件 - 检查参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!