简体   繁体   English

为什么在循环中初始化数组时,数组不包含所有值?

[英]Why do my array doesn't contains all my values when i'm initializing it in a loop?

actually i want to get an array of dates, and in a if statement, i'm setting the i elements of my array to the dates i validated with my if condition. 实际上,我想获取日期数组,并在if语句中,将数组的i元素设置为使用if条件验证的日期。

ls muonic_data|(while read line; do
    myYears[i]=$(echo $line | cut -f1 -d '-')
    myMonths[i]=$(echo $line | cut -f2 -d '-')
    myDays[i]=$(echo $line | cut -f1 -d '_' | cut -f3 -d '-')
    myDate=${myYears[i]}-${myMonths[i]}-${myDays[i]
    echo "Mes dates : " $myDate
    if [[ $myDate =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]];
    then  
            myTab[i]=$myDate
            echo $myTab
    fi

done
echo "Mon tableau : "${myTab[*]}
expect recupFileName.exp ${myTab[@]}
)   

When i run echo at the end of the script, it display me only my lastest date, and not an array of dates. 当我在脚本末尾运行echo时,它仅显示我的最新日期,而不显示日期数组。 I'm really confused there. 我真的很困惑。

root@raspberrypi:~# ./recupFileName.sh
Mes dates :  2017-11-30
2017-11-30
Mes dates :  2017-11-30
2017-11-30
Mes dates :  2018-03-07
2018-03-07
Mes dates :  2018-04-13
2018-04-13
Mes dates :  2018-05-02
2018-05-02
Mes dates :  2018-3-7
Mes dates :  2018-4-13
Mes dates :  2018-5-2
Mon tableau : 2018-05-02

So how can i do to get my date array ? 那么我该如何获取我的日期数组?

Try this 尝试这个

ls muonic_data|(while read line; do
myYears[i]=$(echo $line | cut -f1 -d '-')
myMonths[i]=$(echo $line | cut -f2 -d '-')
myDays[i]=$(echo $line | cut -f1 -d '_' | cut -f3 -d '-')
myDate=${myYears[i]}-${myMonths[i]}-${myDays[i]
echo "Mes dates : " $myDate
var myTab = [];
if [[ $myDate =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]];
then  
        myTab[i]=
        myTab.push("$myDate");
        echo $myTab
fi

done
echo "Mon tableau : printf '%s ' "${myTab[@]}"
expect recupFileName.exp ${myTab[@]}
)   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在循环中创建的数组不包含循环完成后添加的所有值? - Why do the arrays I'm creating within my loop not contain all the values I've added after the loop completes? 如何遍历数组的一部分,为什么我的循环不起作用? - How do I iterate through part of an array and why doesn't my loop work? 为什么我的数组显示循环不打印存储的值? (MIPS组装) - Why doesn't my array display loop print the stored values? (MIPS assembly) 即使所有值都保存在数组中,为什么我的 find 方法在这里不起作用? - Why doesn't my find method work here even with all the values saved on the array? 为什么在循环中我的数组不起作用 - why my array doen't works when is in my loop 为什么我的外循环不增加到数组的下一个元素? - Why my outer loop doesn't increment to the next element of an array? 为什么我的双数组没有初始化为 0? - Why is my double array not initializing to 0? 为什么我的数组不存储任何输入值? - Why doesn't my array store any input values? 为什么在初始化后仍收到消息,表明我的数组为空? - Why do I get a message that my array is empty even after initializing it? JAVA为什么我的对象类型数组元素不接受我指定的所有属性值? - JAVA Why don't my object-type array elements take in all the property values that I assigned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM