【问题标题】:getting error while looping through array in shell在 shell 中循环遍历数组时出错
【发布时间】:2015-05-19 13:13:03
【问题描述】:

我已经编写了一个脚本来从我的目录中 ftp 一组文件,但是我在尝试循环遍历数组时遇到了错误。

#!/usr/bin/ksh
HOST='xxx.xxx.xxx.xxx'   
USER='avio'    
PASSWD='jun'    
FILES[0]=D141203.T024413
FILES[1]=D150101.T012755
FILES[2]=D141203.T024418
echo 'no of files: ' ${#FILES[@]}
ftp -n -v $HOST << EOS
ascii
user $USER $PASSWD
for i in "${FILES[@]}" 
do 
get $i
done
bye
EOS

这是我的 o/p:

no of files: 3
ftp.sh[10]: i: 0403-009 The specified number is not valid for this command.

【问题讨论】:

    标签: arrays shell unix ftp


    【解决方案1】:

    for 循环不作为bash 命令执行;相反,它作为字符串传递给ftp 命令。相反,使用管道将命令的输出提供给ftp:

    #!/usr/bin/ksh
    HOST='xxx.xxx.xxx.xxx'   
    USER='avio'    
    PASSWD='jun'    
    FILES[0]=D141203.T024413
    FILES[1]=D150101.T012755
    FILES[2]=D141203.T024418
    echo 'no of files: ' ${#FILES[@]}
    { echo "ascii"
      echo "user $USER $PASSWD"
      for i in "${FILES[@]}";  do 
         echo "get $i"
      done
      echo "bye"
    } | ftp -n -v $HOST
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-11
      • 2014-12-24
      • 2023-04-10
      • 2019-09-27
      • 1970-01-01
      • 2014-06-04
      相关资源
      最近更新 更多