在另一个批处理脚本中调用一批脚本,并通过一个脚本返回的值进行操作 [英] Call one batch script in another batch script and perform operation on the values returned by first script
问题描述
我有一个批处理脚本first.bat返回值的列表,并退出code。我的任务是调用另一个脚本second.bat这个脚本并仅在第一个脚本中调用还没有返回错误code。通过了first.bat返回的值执行操作。
我已经叫first.bat并保存其在一个文本文件输出。从那以后,我检查结果code和通过阅读文本文件,如果结果code是成功code执行操作。而且手术后我删除文本文件
下面是second.bat的code段
@ECHO OFF
调用first.bat>吨如果/ I%ERRORLEVEL%= = 100( 呼应执行操作 FOR / F标记= 1%%一个在(t)办理回声%%一 FOR / F令牌= 2%%一个在(t)办理回声%%一 德尔ŧ)其他(
发生错误回音
)
我想知道能不能同样的事情在一些优雅的方式通过first.bat的输出不写入文本文件来完成。
@ECHO OFF
SETLOCAL
为%%我在(VAR RETURN1 RETURN2 bat1err)并设置%% i =
FOR / F %% i的('先打电话')DO SET VAR = %%我
FOR / F令牌= 1,2%%我(%VAR%)DO设置RETURN1 = %% I和一套RETURN2 = %%Ĵ
应该建立RETURN1和RETURN2你的两个值BUT值 ERRORLEVEL
按 FIRST.bat
生成可以 T为检索。
在原来的code,文件 T
将在任何情况下产生的 - 而且是零长度如果没有从<$ C $输出C> first.bat 。我被你的行动,只删除混淆 T
如果没有错误发生......
那么 - 我们会真正需要做的是改变 FIRST.bat
一点点
如果 first.bat
不使用SETLOCAL,然后
设置bat1err =%ERRORLEVEL%
在适当的点会返回 bat1err
设置为错误级别。
如果 first.bat
确实使用SETLOCAL,那么生活变得稍微复杂
设置bat1err =%ERRORLEVEL%
在适当的点会设置 bat1err
以同样的方式,但是你需要使用
ENDLOCAL和放大器;设置bat1err =%bat1err%
飞去。这是一个解析绝招,在该行第一次解析,然后执行方式兑现英寸什么情况是,该行作为
实际执行 ENDLOCAL和放大器;设置bat1err = 22
或什么的,设置 BAT1ERR
调用,而不是所谓的批次。的范围内。
另一种方式将包括%ERRORLEVEL%
在你的输出,并且只需更改分析
FOR / F令牌= 1,2,3%%我(%VAR%)DO设置bat1err = %% I和RETURN1 = %% J&安培;集RETURN2 = %%ķ
,或根据相当什么的输出first.bat
是,你可以做到这一点第三种方式:
如果 first.bat
不产生输出的错误,但成功输出的一条线,
FOR / F %% i的('先打电话')DO SET VAR = %%我
如果定义VAR(
FOR / F令牌= 1,2%%我(%VAR%)DO设置RETURN1 = %% I和一套RETURN2 = %%Ĵ
)其他(回声发生错误)
和再收益
, RETURN2
和 VAR
CAN与分析如果定义
来作出决策。
这真的取决于我们没有的信息。
I have a batch script "first.bat" that returns a list of values and an exit code. My task is to call this script in another script "second.bat" and perform the operations on the values returned by the "first.bat" only if the calling of first script has not returned an error code.
I have called the first.bat and stored its output in a text file. After that I check the result code and perform the operation by reading the text file if the result code is success code. And after the operation I delete the text file
Below is the code snippet of second.bat
@ECHO OFF
call first.bat >t
if /i %errorlevel%==100 (
echo Performing operation
for /F "tokens=1" %%a in (t) do echo %%a
for /F "tokens=2" %%a in (t) do echo %%a
del t
) else (
echo Error occurred
)
I want to know can the same thing be done in some elegant way by not writing the output of first.bat to text file.
@ECHO OFF
SETLOCAL
for %%i in (var return1 return2 bat1err) do set "%%i="
FOR /f %%i IN ('call first') DO SET var=%%i
FOR /f "tokens=1,2" %%i IN ("%var%") DO set return1=%%i&set return2=%%j
Should set return1 and return2 to your two values BUT the value of ERRORLEVEL
generated by FIRST.bat
can't be retrieved.
In your original code, the file t
would be generated in any case - and would be zero-length if there was no output from first.bat
. I'm confused by your action in only deleting t
if no-error-occurs....
So - what we'd really need to do is to change FIRST.bat
a little.
If first.bat
does not use SETLOCAL, then
set bat1err=%errorlevel%
at an appropriate point would return bat1err
set to the errorlevel.
If first.bat
does use SETLOCAL, then life gets a little more complicated
set bat1err=%errorlevel%
at an appropriate point would set bat1err
in the same way, but you would need to use
ENDLOCAL&set bat1err=%bat1err%
before exiting. This is a parsing trick, cashing in on the way in which the line is first parsed, then executed. What happens is that the line is actually executed as
endlocal&set bat1err=22
or whatever, setting BAT1ERR
within the context of the calling, not the called batch.
Another way would be to include %errorlevel%
in your output, and simply change the analysis to
FOR /f "tokens=1,2,3" %%i IN ("%var%") DO set bat1err=%%i&return1=%%j&set return2=%%k
OR, depending on quite what the output of first.bat
is, you may be able to do it a third way:
If first.bat
produces no output for error but a line of output for success,
FOR /f %%i IN ('call first') DO SET var=%%i
if defined var (
FOR /f "tokens=1,2" %%i IN ("%var%") DO set return1=%%i&set return2=%%j
) else (echo Error occurred)
and again return
, return2
and var
can be analysed with IF DEFINED
to make decisions.
That really depends on information we don't have.
这篇关于在另一个批处理脚本中调用一批脚本,并通过一个脚本返回的值进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!