在此处文档后使用 bash 语法

Use bash syntax after a here document

我需要创建一个连接到 ftp 服务器的脚本,并检查一些备份的大小并将它们与本地备份的大小进行比较。 我的脚本如下所示:

#!/bin/bash

cd /var/archives/
j=0
array=( $(find -type f -name "*.gz") )
for i in "${array[@]}"
do
    gunzip -c $i | tar t > /dev/null
    if [ "$?" -ne "0" ]
    then
    echo "The file $i might be corrupted. You might check it"
    exit 1
    fi
    size[$j]=$(stat -c%s "$i")
    echo "${size[$j]}"
    ((j+=1))
done
echo "BackUp is correct. Checking md5sum..."
ftp -inv $ftp_host << EOF
quote USER $ftp_login
quote PASS $ftp_password
binary
cd $ftp_path
j=0
array=( $(find -type f -name "*.gz") )
for i in "${array[@]}"
do
    size[$j]=$(stat -c%s "$i")
    echo "${size[$j]}"
    ((j+=1))
done
EOF

但它只是在我的 ftp...

中输入 here document 类命令的内容

那么在这种情况下我该如何做循环和条件呢?

我刚刚在你的帮助下完成了我的剧本! 我post可能有些人会感兴趣。

#!/bin/bash

ftp_host=localhost
ftp_login=*********
ftp_password=*******
ftp_path="**********$"
nbr_file=2

cd /var/archives/
j=0
array=( $(find -type f -name "*.gz") )
for i in "${array[@]}"
do
    gunzip -c $i | tar t > /dev/null
    if [ "$?" -ne "0" ]
    then
    echo "The file $i might be corrupted. You might check it"
    exit 1
    fi
size[$j]=$(stat -c%s "$i")
((j+=1))
done

echo "${size[@]}" > local_output

echo "open $ftp_host
user $ftp_login $ftp_password
binary
cd $ftp_path" > /tmp/ftp.$$

i=0
while [ $i -ne $j ]
do
    echo "size ${array[$i]}" >> /tmp/ftp.$$
    ((i+=1))
done
echo "quit" >> /tmp/ftp.$$
ftp -ivn < /tmp/ftp.$$ > ftp_output
rm /tmp/ftp.$$
cd /root/backup/
./compare-size $nbr_file
if [ "$?" -eq "1" ]
then
exit 1
else
echo "The backup seems to be good !"
exit 0
fi