从从PuTTY运行到调用批处理文件的Shell脚本返回代码? [英] Return code from shell script run from PuTTY to calling batch file?

查看:307
本文介绍了从从PuTTY运行到调用批处理文件的Shell脚本返回代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个有关如何应用PuTTY并控制其返回值的问题.我的设置就是这样.

I would like to ask a question about how to apply PuTTY and control its return values. My setup is like this.

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

There's a xxxxx.bat file which contains PuTTY call with a ssh connection in the likes of:

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文件中特定命令的返回值,并从bat文件中调用该值,以将其添加到if中.循环显示警告,即if $?(或%ERRORLEVEL-我不知道该怎么用)then BLABLA

The file lintest.bsh contains various commands, what interests me is to be able to capture the return value of a specific command in the .bsh file, and call on that value from the bat file, to add it into an if loop with a warning, ie if $? (or %ERRORLEVEL - I don't know what would work) then BLABLA

我已经阅读了很多有关此的文章,但是坦率地说,这是我第一次使用.bat文件进行操作,因此它们都有些混乱.

I've read a lot of posts about this, but frankly this is my first time doing anything with .bat files so its all slightly confusing.

推荐答案

首先,不要将PuTTY用于自动化,请使用

First, do not use PuTTY for automation, use Plink (PuTTY command-line connection tool).

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

But even Plink cannot propagate the remote command exit code.

您可以让远程脚本在其输出的最后一行上打印退出代码(您正在使用echo $?做的事情),并使批处理文件解析退出代码:

You can have the remote script print the exit code on the last line of its output (what you are doing already with the echo $?) and have the batch file parse the exit code:

@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
)

但是,当然,您必须修复脚本以返回所需的退出代码(当前代码返回上一个echo命令的退出代码):

But of course, you have to fix your script to return the exit code you want (the current code returns exit code of the previous echo command):

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

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

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

这篇关于从从PuTTY运行到调用批处理文件的Shell脚本返回代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆