2014-12-31 76 views
1

我需要检查在远程主机上发布了一个transacions文件。今天12月31日没有发布交易代码。 我知道这是确定的。昨天有一个。然而,无论我使用哪个日期,30或31,返回码都是相同的。在bash中检查ssh remote命令的退出状态

我正在查看的是ssh的返回状态,我认为应该是远程执行命令的返回状态 我认为如果'ls -ltr'工作,'if语句'会以0退出和大于零的东西,如果'ls -ltr'没有 工作,则为false。我会认为'if'语句会查看shh的返回状态,这应该是远程执行的命令的返回状态 - 或者'ls -ltr'

我如何获得这个工作?

这是针对30 - 这是在远程主机上,并且是成功的

[email protected]:/scripts$ ssh -q -T [email protected] "ls -ltr /home/DropBox/transactions_20141230.csv" 
-rw-r--r-- 1 rr_prd 2233047 Dec 31 07:26 /home/DropBox/transactions_20141230.csv 

,这是对31没有报告还为31,所以它是不成功的

[email protected]:/scripts$ ssh -q -T [email protected] "ls -ltr /home/DropBox/transactions_20141231.csv" 
/home/DropBox/transactions_20141231.csv not found 
[email protected]:/scripts$ 

我不停地翻转与注释掉包括hashtag

[email protected]:/scripts$ ssh -q -T [email protected] "ls -ltr /home/DropBox/transactions_20141231.csv" 
/home/DropBox/transactions_20141231.csv not found 
[email protected]:/scripts$ 



#!/bin/bash 
today=$(/bin/date +%Y%m%d) 
#today="20141230" 
echo $today 
if ssh -q -T [email protected] "ls -ltr /home/DropBox/transactions_$today.csv" > /dev/null 2>&1 ; 
    then 
     echo "this worked" 
    else 
     echo "this did not work" 
fi 

但是日期 - 当我使用脚本两个日期 - 它是成功的,当为31,它真的应该返回“这并不工作”

[email protected]:/scripts$ 
[email protected]:/scripts$ vim offshore_check 
[email protected]:/scripts$ ./offshore_check 
20141230 
this worked 
[email protected]:/scripts$ vim offshore_check 
[email protected]:/scripts$ ./offshore_check 
20141231 
this worked 
[email protected]:/scripts$ 
[email protected]:/scripts$ 

回答

0

“如果”构建你使用的应该工作,它为我工作:

$ if ssh -q -T localhost "ls -ltr /does/not/exist"; then echo succeeded; else echo failed; fi 
Password: 
ls: /does/not/exist: No such file or directory 
failed 

我注意到,你ls程序打印的错误不是错误措辞不同我得到了(我尝试了两种不同的系统)。在你的情况下,“文件未找到”,在我的“ls:file:没有这样的文件或目录”。我的怀疑是你在这里调用的ls命令不是典型的现代Unix命令。您可能正在运行非标准版本,非Unix版本或非常旧的版本,并且对于此特定错误,它可能实际上不会退出,且具有非零退出代码。

+0

它是一个较旧的离岸机器 - - SunOS 5.8 Generic_117350-35 - build-version 020 – capser

0

类似的问题在这里已经讨论过很多次,看到scp return code discussion 1

和300+答案当searching for scp errors

,但给你一个工作的答案,考虑尝试这种

if 
    ssh -q -T [email protected] "ls -ltr /home/DropBox/transactions_${today}.csv" \ 
    | grep -q "transactions_${today}\.csv" ; 
then 
     echo "this worked" 
else 
     echo "nope" 
fi 

对不起,我没有任何办法来测试这一点。 IHD。

IHTH。

1
file="/home/DropBox/transactions_20141231.csv" 
ssh -q -T [email protected] "test -e $file" 
ret="$?" 
case $ret in 
    0) 
    echo "$file exists" 
    ;; 
    1) 
    echo "$file does not exist" 
    ;; 
    *) 
    echo "other problem" 
    ;; 
esac 
相关问题