2014-03-19 42 views
0

我正在閱讀配置文件中的文件名列表。使用for循環即時選擇列表中的每個文件,然後要在遠程服務器上搜索文件。Unix命令檢查另一臺服務器上是否存在文件?

for file in config 
do 

**search for file at {remoteserver}** 
    returnStatus=$? 
    if [ returnStatus = 0 ]; then 
     email that file was found. 
    fi 
done 

在遠程服務器上搜索文件是困擾我的部分。任何幫助表示讚賞,

+0

你檢查了這個問題嗎? [如何檢查目錄是否存在於shell腳本中](http://stackoverflow.com/q/59838/1983854)。與'ssh'一起做。 – fedorqui

回答

2

你可以這樣做:

for file in "${config[@]}" 
do 
    ssh [email protected] test -e "$file" 
    returnStatus=$? 
    if [ $returnStatus -eq 0 ]; then 
     email that file was found. 
    fi 
done 
+0

謝謝,我會試試這個! – Blue42

+0

我得到:「返回:只能從函數或源腳本'返回'」。 –

+0

@TomFenech我打算寫'退出'不回來。固定。 –

1

假設

  1. 所有的文件都是在獨立的服務器
  2. 你想從本地系統發送電子郵件

這可能是解決方案:

for file in "${config[@]}" 
do 
    exists=$(ssh [email protected] "if [ -e \"$file\" ]; then echo '1'; else echo '0'; fi") 
    if [ $exists = "1" ] 
    then 
     echo "$file exists" 
     #send email 
    fi 
done 

如果所有文件都在同一個遠程服務器上,那麼在ssh連接中執行循環會更有意義,而不是每次重新連接。

+0

只是想感謝您幫助調試腳本並修復它。正如你所看到的,我用'bash'不是那麼棒。希望能很快到達那裏。欣賞它。 +1 –

相關問題