2017-01-26 27 views
0

我正在使用Ubuntu 14並嘗試從遠程服務器導入文件。該文件是一個SQL,並在那裏有很多。在bash中使用argrument在遠程服務器上查找文件

我在本地服務器上使用此腳本,它返回文件名。

#!/bin/bash 

input=$1 
echo $input 

#get the file name of the most recent file 
file=`find /home/user/dbBackups -type f -name ${input}"*" | sort -n | tail -1 | cut -f1,2 -d'_'` 

file1=$(basename $file) 
echo $file1 

然後我試着把它放到另一臺電腦上,試圖用它遠程執行它。

#!/bin/bash 

input=$1 
echo $input 

file=$(ssh [email protected]<servername> -p 12345 `find /home/user/dbBackups -type f -name ${input}"*" | sort -n | tail -1 | cut -f1,2 -d'_'`) 
file1=$(basename $file) 
echo $file1 

當試圖在遠程服務器上運行腳本時,它掛在命令上,我無法「ctrl c」出它。我認爲這是find命令中的變量。如果我用文件名替換它,它就可以。

我對編碼世界相當陌生,這是我的第一個StackExchange文章。希望我已經提供了足夠的細節。

在此先感謝

回答

0

隨着反引號,你就是你本地機,而不是遠程機器上執行find命令。你想:

remote_cmd="find /home/user/dbBackups -type f -name '$input*' | sort -n | tail -1 | cut -f1,2 -d'_'" 
file=$(ssh [email protected]<servername> -p 12345 sh -c "$remote_cmd") 

但是,如果你想轉移文件,您的答覆使用scp

+0

非常感謝。 我確實希望在下一部分中檢索該文件。我可以使用$文件作爲命令所需的部分。 或者我可以在scp命令中使用$ remote_cmd嗎? –

+0

此外,它似乎只在用戶的主目錄中搜索。並且在'find'之後沒有使用在remote_cmd變量中發出的路徑。 仍然有一個想法,看看我是否可以弄明白。 –

相關問題