我正在创建一个基于此脚本的脚本。但是当我在结果上运行它时,如果几个月不相同,它会删除比日期更新的文件。
Linux shell script for delete old files from ftp
"删除02-02-2015"这是我今天创建的备份
#!/bin/bash
# get a list of directories and dates from ftp and remove directories older than ndays
ftpsite="hostname"
ftpuser="user"
ftppass="pass"
putdir="/full"
ndays=15
# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
echo removing files older than $MM $DD
# get directory listing from remote source
listing=`ftp -i -n -p $ftpsite <<EOMYF
user $ftpuser $ftppass
binary
cd $putdir
ls
quit
EOMYF
`
lista=( $listing )
# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
# echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} File: ${lista[`expr $FNO+8`]}
# check the date stamp
if [ ${lista[`expr $FNO+5`]}=$MM ];
then
if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
then
# Remove this file
echo "Removing ${lista[`expr $FNO+8`]}";
ftp -i -n -p $ftpsite <<EOMYF2
user $ftpuser $ftppass
binary
cd $putdir
mdelete ${lista[`expr $FNO+8`]}/*
rmdir ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
done
result =&gt; 删除早于1月18日的文件 删除02-02-2015
答案 0 :(得分:1)
这条线似乎是错误的,因为它总是如此:
if [ ${lista[`expr $FNO+5`]}=$MM ];
你应该在“=”周围写一些空格。正确的形式是:
if [ ${lista[`expr $FNO+5`]} = $MM ];
[ ]
是bash内置的,其参数将传递给“test”命令。 [ asd = "$qwe" ]
相当于:test "asd" "=" "$qwe"