[英]korn shell script failing
在下面的korn shell中需要幫助。 語句if (( ${lista[expr $FNO + 5]} == $MM ));
因語法錯誤而失敗。 正在獲取==的語法錯誤。 也嘗試過-eq,但問題相同。
這將不返回任何內容: ${lista[expr $FNO + 5]}
。 如何在這里獲得月份值?
#!/bin/ksh
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="xxx.com"
ftpuser="ftpuser"
ftppass="ftppwd"
putdir="/ftpdir"
MM=`TZ=GMT+29 date +%b`
DD=`TZ=GMT+29 date +%d`
echo removing files older than $MM $DD
# get directory listing from remote source
listing=`ftp -i -n $ftpsite <<EOMYF
user $ftpuser $ftppass
binary
cd $putdir
ls -l
quit
EOMYF`
lista=$listing
# loop over our files
FNO=0
while (( $FNO < ${#lista[@]} )); 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`]}"
echo "Removing ${lista[`expr $FNO + 8`]}"
ftp -i -n $ftpsite <<EOMYF2
user $ftpuser $ftppass
binary
cd $putdir
delete ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
(( FNO = $FNO + 9 ))
done
請為此提供幫助。
謝謝您的幫助。 是的,我是從pastebin本身復制過來的。 當我嘗試使用ls * .txt獲取以下輸出時,它沒有得到$ 5的值,因此,根據您的建議,嘗試使用ls -l * .txt。 我還能嘗試什么?
下面是我從pastebin本身復制的腳本
#!/bin/ksh -px
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="ftp.com"
ftpuser="ftpuser"
ftppass="ftppass"
putdir="/xxx/yyy"
MM=$(TZ=GMT+29 date +%b)
DD=$(TZ=GMT+29 date +%d)
echo "removing files older than $MM $DD"
# get directory listing from remote source
echo "
user $ftpuser $ftppass
binary
cd $putdir
ls *.txt
quit" | ftp -i -n $ftpsite | while read line; do
# line is *each line*, rather than a list of everything
# helps if the filenames have spaces. Just chomp into $0..
set -- $line
# month (element 5), day (element 6) and filename (element 8)
if (($# > 9)); then
echo "Can't deal with '$@'"
continue
fi
# check the date stamp
if [[ $5 == "$MM" ]];
then
if (($6 < $DD ));
then
# Remove this file
filename=$8
echo "Removing $filename"
# replace the ftp with cat with ftp $ftpsite
cat <<EOMYF2
ftp -n -i $ftpsite
user $ftpuser $ftppass
binary
cd $putdir
delete $filename
quit
EOMYF2
fi
fi
done
當您分配: lista=$listing
,這是一個簡單的變量分配。
您想要將listing
作為數組分配給lista
。 其語法為:
set -A lista $listing
順便說一句,我會使用
$()
代替反引號來進行命令調用,並使用$((math))
代替expr math
,因為它們更加ksh流暢。
嘗試調試ftp文件列表:
ftpsite="xxx.com"
ftpuser="ftpuser"
ftppass="ftppwd"
putdir="/ftpdir"
ftp -i -n $ftpsite <<EOM
user $ftpuser $ftppass
binary
cd $putdir
ls -l *.txt
quit
EOM
這將列出所有要考慮的文件-您需要確保它們的格式相同。
確保格式符合每行預期的9個項目之后,即可嘗試調試腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.