当我的Windows .bat被调用时,ECHO是ON还是OFF?

时间:2018-01-24 10:23:51

标签: windows batch-file variables echo

当我CALL一个来自另一个的.bat个文件时,被叫文件可以告诉来电者中ECHOON还是OFF

输入.bat文件后,ECHO设置为ON(即使CALLER中为OFF)。

我想在被叫ECHO中保留来电者status .bat,这样我就可以控制两个(可能是两个以上嵌套调用的链)的地方。

我可以传递参数或设置我自己的环境变量但是有更好的方法吗?

修改 只是为了澄清所需要的: callee.cmd只需要在运行期间使用echo off。返回其调用者后,它必须恢复调用者的回显状态。

请在下面的脚本中填写????? s:

callee.cmd:

@rem save the echo state
@ ???? HOW???? ????
@ set "savedEchoState=??????"

@rem the following commands must not be echoed
@echo off
command1
command2
command3

@rem restore the previous echo state
echo %savedEchoState%

caller1.cmd:

@echo off
call callee.cmd
echo

caller2.cmd:

@echo on
call callee.cmd
echo

必需的输出:

caller1.cmd必须打印echo off,caller2.cmd必须打印echo on

是否可以完成而不在磁盘上创建任何文件

2 个答案:

答案 0 :(得分:3)

这是一个艰难的。涉及管道或for的所有内容都不起作用,因为生成了新进程,这会使echo状态无效。可能涉及临时文件:

A.BAT:

echo on
echo
call b.bat
echo off
echo
call b.bat
echo

B.BAT:

@(>tmp echo)
@type tmp|find "ON" >nul && (@set "oldEcho=ON") || (@set "OldEcho=OFF")
@echo off
echo B:echo was previously %OldEcho%
REM insert your payload here
REM restore previous Echo Status:
echo %OldEcho%

结果:

C:\Folder>a

C:\Folder>echo on

C:\Folder>echo
ECHO ist eingeschaltet (ON).

C:\Folder>call b.bat
B:echo was previously ON

C:\Folder>echo off
ECHO ist ausgeschaltet (OFF).
B:echo was previously OFF
ECHO ist ausgeschaltet (OFF).

答案 1 :(得分:2)

要检测ECHO状态而不依赖于Windows语言,您可以使用临时文件 当ECHO为OFF时,该文件为空,否则它包含语言字符串ECHO IS ON

@echo off
echo Testing detectEchoState

echo on
@call :detectEchoState

@echo off
@call :detectEchoState off
exit /b


:detectEchoState
@(
    (
        FOR /F %%L in ("1") do REM
    ) > "%TEMP%\echoCheck.tmp"

    FOR /F "delims=" %%F in ("%TEMP%\echoCheck.tmp") do @if %%~zF == 0 (set "EchoState=OFF" ) ELSE (set "EchoState=ON")
    del "%TEMP%\echoCheck.tmp"
    echo EchoState !EchoState!
)
@exit /b