【问题标题】:Bash: Array empty outside of the for loopBash:for循环外的数组为空
【发布时间】:2019-01-21 02:24:35
【问题描述】:

我正在创建一个脚本来轮换我的备份,该脚本的一个重要部分是根据特定条件查找文件,然后移动或删除它们。

我已经开始循环了,但是数组(path_array)不能在循环外使用。我已经阅读了几篇关于此的帖子,但不确定答案如何适用于我的具体案例。请参阅下面的代码。

谢谢!!!

#!/bin/bash

# anos=(2016 2017 2018 2019)
# meses=(02)

meses=(01 02 03 04 05 06 07 08 09 10 11 12)
anos=(2018)




source="/volume1/NetBackup/Servers/MIA/"
destination="/volume1/NetBackup/Servers/MIA/_Archive"  



######## Pasar los Files del Primer dia del Año a Archiving

for i in  ${anos[@]}; do
    for j in  ${meses[@]}; do
        month_start=$(date +$i-$j-01)
        month_finish=$(date +$i-$j-02)
        # echo $month_start
        # echo $month_finish
        path_array=(`find $source -type f -not -path "*/_Archive/*" -newermt $month_start ! -newermt $month_finish | cut -sd / -f 6-`)
        # echo $path_array  

        # echo Archivos año: $i mes: $j

        #printf '%s\n' "${path_array[@]}"
    done
done


 printf '%s\n' "${path_array[@]}"

【问题讨论】:

  • 在您的代码中,path_array is 在循环之外可用。例如,在 bash 中尝试命令 for i in a; do for j in b; do arr=($i $j); done; done; echo ${arr[1]},您将看到它输出 b.

标签: arrays bash loops variables


【解决方案1】:

我猜你想追加到数组

path_array+=( $(find $source -type f -not -path "*/_Archive/*" -newermt $month_start ! -newermt $month_finish | cut -sd / -f 6-) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多