2017-10-16 53 views
0

我從遠程計算機使用SFTP複製一些文件到我的機器:比較文件大小從遠程計算機上的bash

ssh [email protected]_host ' 
find /file/location -type f -maxdepth 1 -size +400000c -name "TEXT_*" 
'>> log 

cat log | while read line; do 

sftp [email protected]_host <<EOF 
get $line 
EOF 

done 

rm log 

我想現在要做的是檢查,如果複製的文件的文件大小是與遠程機器上的文件相同。這是我的嘗試:

ls -1 TEXT* | while read line; 
do var=$(stat "/file/on/current/machine/$line"); 
    var2=`ssh -n [email protected]_host 'ls -l /file/on/remote/$line | cut -d " " -f5 '`; 
     if [ "$var" == "$var2" ]; 
     then 
       echo "The file $line has the same dimension!"; 
     else 
       echo "The file $line doesn't have the same dimension"; 
     fi; 
done 

的問題是變量$行不會ssh命令裏面的認可,並將其返回遠程主機上的所有文件,不僅已複製的那些的大小。

謝謝!

回答

1

這是因爲$line在單引號內。變量只能用雙引號插入。

var2=$(ssh -n [email protected]_host "ls -l /file/on/remote/$line | cut -d ' ' -f5 ")