linux - korn shell 脚本失败

标签 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 美元的值(value),因此,根据您的建议尝试 ls -l *.txt。我还能尝试什么?

  • 设置--ccc_20160301.txt
  • (( 1 > 9 ))
  • [[ == 二月 ]]
  • 读行

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

BTW, I'd use $() instead of backticks for command invocation, and use $((math)) instead of expr math, as they're more ksh-fluent.

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

关于linux - korn shell 脚本失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35619050/

相关文章:

linux - 在 Mac OS 上设置 jenkins slave

c++ - Qt5 静态链接在 Ubuntu Linux 上失败

linux - 如何让 GNU screen 读取 .bash_profile/.bash_rc 更改?

android - 如何在 Android 中 ping 时获取 Unix 时间戳

bash - "2<&1"重定向在 Bourne shell 中有什么作用?

arrays - 用于检查数组是否为空并重新启动程序(如果为空)的 shell 脚本

linux - 当有人连接到特定数据库时如何获得弹出窗口

c++ - 重定向标准 I/O : freopen + stdin/stdout vs open + dup2

shell - 查找在 shell 中的另一个文件的五天内创建的文件

linux - 跨主机和 Docker 容器环境管理目录权限