korn shell脚本失败

时间:2016-02-25 05:25:06

标签: linux unix sh

需要帮助以下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美元的价值因此,根据你的suggession尝试了ls -l * .txt。我还能尝试什么?

  • set - ccc_20160301.txt
  • ((1> 9))
  • [[== Feb]]
  • 读取行

以下是我从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

1 个答案:

答案 0 :(得分:0)

分配:lista=$listing时,这是一个简单的变量赋值。

您想将listing指定为lista作为数组。其语法是:

set -A lista $listing
  

顺便说一下,我使用$()代替反引号进行命令调用,并使用$((math))代替expr math,因为它们更流畅。

尝试调试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个项目,您就可以尝试调试脚本。