2015-01-02 156 views
2

我做了这个脚本来检查以确保创建了离岸脚本。我工作正常&emdash;我加入了一个使它更清洁的功能。但我不认为这个功能可以工作,因为它可以在几秒钟内同时打印出来。在while循环中调用Bash函数

#!/bin/bash 
#set -x 
check_offshore() 
{ 
    local RESULTS 
    RESULTS=$(ssh -q -T [email protected] "ls -ltr /come/and/play/with/us/danny/DropBox| grep foreverr_and_$today.csv") 
    rc=$? 
} 

today=$(/bin/date +%Y%m%d) 
time=$(/bin/date +"%T.%3N") 
iterate=0 
while [ $iterate -le 5 ] 
do 
    check_offshore 
    if [[ $rc != 0 ]] ; then 
     echo "$time foreverr_and_$today.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox" 
     fi 
    sleep 5 
    iterate=$((iterate+1)) 
    done 

这是它创建的日志&emdash;日志时间永远不会改变。它一直保持不变,直到剧本停止。

17:42:28.380 foreverr_and_20150102.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox 
17:42:28.380 foreverr_and_20150102.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox 
17:42:28.380 foreverr_and_20150102.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox 
17:42:28.380 foreverr_and_20150102.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox 
17:42:28.380 foreverr_and_20150102.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox 
17:42:28.380 foreverr_and_20150102.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox 

这是怎么回事,我该如何解决它?

+3

既然你没有在循环内设置'time = $(...)',你怎么期望它改变呢? –

+0

如果你使用Bash,你可能更喜欢'((iterate = 0; iterate <= 5; iterate ++))'来控制循环。 –

回答

3

请注意,问题与此功能无关;麻烦在于环路及其周围环境。

由于您没有在循环内设置time=$(...),您如何期望它发生变化?

修改循环,以便在每次迭代中重新评估时间。由于日期可能会在23:59:30之后运行此脚本时发生更改,因此您可能还需要在每次迭代中设置日期。您还可以使用特定的Bash回路控制机制:

for ((iterate = 0; iterate <= 5; iterate++)) 
do 
    today=$(/bin/date +%Y%m%d) 
    time=$(/bin/date +"%T.%3N") 
    check_offshore 
    if [[ $rc != 0 ]] ; then 
     echo "$time foreverr_and_$today.csv is not present in [email protected]:/come/and/play/with/us/danny/DropBox" 
    fi 
    sleep 5 
done 

你可能更愿意坚持使用原来的日期,在这种情况下,您可以继续设置today一次外循环。请注意,如果事情从23:59:56.234到00:00:02.197变得有点可疑,因为事情在午夜之前需要一段时间。无论对你而言,重要的是你必须做出的判断。我建议在日志文件中明确使用日期+时间;在几天后诊断发生的事情更容易。

+0

timmy() { time = $(/ bin/date +“%Y%m%d-%T.%3N”) } 增加了一个带日期和时间的子程序 - 谢谢 – capser