Return 来自 shell 脚本的代码 运行 从 PuTTY 到调用批处理文件?

Return code from shell script run from PuTTY to calling batch file?

我想问一个关于如何应用 PuTTY 并控制其 return 值的问题。我的设置是这样的。

有一个 xxxxx.bat 文件,其中包含带有 ssh 连接的 PuTTY 调用,例如:

putty.ext ssh 10.10.10.10 -l xxxx -pw yyyy -t -m wintest.txt

文件 wintest.txt 包含:

echo "wintest.txt: Before execute..."

/home/tede/n55115/PD/winlinux/RUN.lintest.bsh

echo "wintest.txt: After execute..."
echo $?

文件lintest.bsh包含各种命令,我感兴趣的是能够捕获.bsh文件中特定命令的return值,并从bat调用该值文件,将其添加到带有警告的 if 循环中,即 if $?(或 %ERRORLEVEL - 我不知道什么会起作用)then BLABLA

我已经阅读了很多关于此的帖子,但坦率地说,这是我第一次对 .bat 文件做任何事情,所以有点混乱。

首先不要使用PuTTY做自动化,使用Plink(PuTTY命令行连接工具)

但即使是 Plink 也无法传播远程命令退出代码。

您可以让远程脚本在其输出的最后一行打印退出代码(您已经使用 echo $? 执行的操作)并让批处理文件解析退出代码:

@echo off

plink.exe putty.ext ssh 10.10.10.10 -l xxxx -pw yyyy -t -m wintest.txt > output.txt 2>&1

for /F "delims=" %%a in (output.txt) do (
   echo %%a
   set "SHELL_EXIT_CODE=%%a"
)

if %SHELL_EXIT_CODE% gtr 0 (
   echo Error %SHELL_EXIT_CODE%
) else (
   echo Success
)

但是,当然,您必须将您的脚本修复为 return 您想要的退出代码(当前代码 returns 先前 echo 命令的退出代码):

echo "wintest.txt: Before execute..."

/home/tede/n55115/PD/winlinux/RUN.lintest.bsh
EXIT_CODE=$?

echo "wintest.txt: After execute..."
echo $EXIT_CODE