简体   繁体   English

使用Teraterm API调用“ setexitcode”来设置WIndows系统变量%errorlevel%需要2个teraterm会话进行注册

[英]Using teraterm API call 'setexitcode' to set WIndows system variable %errorlevel% requires 2 teraterm sessions to register

The following 4 files can be used to demonstrate the problem I am seeing: (each simplified for readability.) 以下4个文件可用于演示我遇到的问题:(为简化可读性,每个文件都进行了简化。)

temp1.bat: temp1.bat:

@echo off
start /w ttpmacro.exe c:\temp\temp.ttl 0 
start /w ttpmacro.exe c:\temp\no_op.ttl && if %errorlevel% EQU 0 (echo PASS ) Else ( Echo TIMEOUT )

temp2.bat: temp2.​​bat:

@echo off
start /w ttpmacro.exe c:\temp\temp.ttl 1 && if %errorlevel% EQU 0 (echo PASS ) Else ( Echo TIMEOUT )  

Note: the digit following c:\\temp\\temp.ttl (eg a 1 or 0 in these examples.) is a command line paramter, an is referred to as param2 within the script. 注意: c:\\ temp \\ temp.ttl之后的数字(例如,在这些示例中为10 )是命令行参数,在脚本中将其称为param2

temp.ttl temp.ttl

str2int num param2
setexitcode num  

no_op.ttl no_op.ttl

;Do nothing
num = 0  

Running temp1.bat from a Windows CMD prompt, the comparison statement yields the expected response each time, consistently. 从Windows CMD提示符运行temp1.bat,比较语句每次都会始终产生预期的响应。

Running temp2.bat from a Windows CMD prompt, the comparison statement result is consistently unexpected after the first run, but when called a second time yields the expected result. 从Windows CMD提示符运行temp2.​​bat,第一次运行后比较语句的结果始终是意外的,但是第二次调用时会产生预期的结果。

The problem I am having is that Windows does not seem to register the value into its %errorlevel% variable upon exiting a single instance of a teraterm script. 我遇到的问题是,退出一个Teraterm脚本的单个实例时,Windows似乎没有将值注册到其%errorlevel%变量中。 I have to run a subsequest script (eg. no-op.ttl ) before the %errorlevel% variable is updated, and the comparison test yields the expected results. 在%errorlevel%变量更新之前,我必须运行subsequest脚本(例如, no-op.ttl ),并且比较测试会产生预期的结果。

Can someone identify how I can have the Windows %errorlevel% variable updated correctly after running a single instance of the Tera Term script? 有人可以确定在运行Tera Term脚本的单个实例后如何正确更新Windows%errorlevel%变量吗?

Additional information on key items in Tera Term script and batch files : 有关Tera Term脚本和批处理文件中关键项的附加信息

Usage - TTPMACRO 用法-TTPMACRO

setexitcode (From the Tera Term API reference documentation.) enables a script to return a value to the calling process, eg in this case a CMD window. setexitcode (来自Tera Term API参考文档。)使脚本能够将值返回到调用过程,例如,在这种情况下为CMD窗口。 I am using this returned value to make determine within Windows, that all internal script steps were executed correctly. 我正在使用此返回值来确定Windows中所有内部脚本步骤均已正确执行。 (all but those lines essential for this question have been removed.) (已删除该问题所必需的所有行,但所有行均已删除。)

And, usage information on Errorlevel states: 并且,有关错误级别的用法信息指出:

A preferred method of checking Errorlevels is to use the %ERRORLEVEL% variable: 检查错误级别的一种首选方法是使用%ERRORLEVEL%变量:

IF %ERRORLEVEL% NEQ 0 Echo An error was found
IF %ERRORLEVEL% EQU 0 Echo No error found

IF %ERRORLEVEL% EQU 0 (Echo No error found) ELSE (Echo An error was found)
IF %ERRORLEVEL% EQU 0 Echo No error found || Echo An error was found
start /w ttpmacro.exe c:\temp\temp.ttl 1 && if errorlevel 1 (echo TIMEOUT ) Else ( Echo PASS ) 

When cmd parses a block of code, it replaces any %var% with the current value of that variable and then executes the code. cmd解析代码块时,它将用该变量的当前值替换任何%var%然后执行代码。

Consequently, %errorlevel% is being replaced by the value returned by the previous run. 因此, %errorlevel%被上一次运行返回的值替换。

This is the conventional way of using errorlevel - IF ERRORLEVEL n is TRUE if the runtime (ie. current) errorlevel is n or greater than n . 这是使用错误errorlevel的常规方法-如果运行时(即当前)错误errorlevel为n 或大于 IF ERRORLEVEL nIF ERRORLEVEL n为TRUE。 IF ERRORLEVEL 0 is therefore always true. IF ERRORLEVEL 0因此始终为true。 IF NOT ERRORLEVEL 1 is a test for errorlevel=0. IF NOT ERRORLEVEL 1是对errorlevel = 0的测试。 So is IF %ERRORLEVEL%==0 , except that the former can be used within a block but the latter cannot. IF %ERRORLEVEL%==0 ,除了前者可以在一个块内使用而后者不能使用。

There are many items on SO about delayedexpansion - another method that could be used. SO上有很多关于delayedexpansion -可以使用的另一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM