简体   繁体   English

如何在bash脚本中清空数组

[英]How to empty an array in bash script

I am trying to get specific information from a bunch of files.我试图从一堆文件中获取特定信息。 Iterating over a list of files, grep ing for what I need.遍历文件列表grep我需要什么ING。 I know for sure that each grep will give more than 1 result and I want to store that result in an array.我确信每个grep都会给出 1 个以上的结果,我想将该结果存储在一个数组中。 After finishing the work specific to file, I want to erase everything from arrays and start afresh for new file.完成特定于文件的工作后,我想从数组中擦除所有内容并重新开始新文件。

files_list=`ls`

for f in $files_list
do
        echo $f
        arr1=`cat $f | grep "abc" | grep "xyz"`
        arr2=`cat $f | grep "pqr" | grep "mno"`
        arr3=`cat $f | grep "df"`
        for ((i=0; i<${#arr1[@]}; ++i)) 
        do
            printf "%s  %s %s\n" "${arr1[i]}" "${arr2[i]}" "${arr3[i]}"
        done
        unset $arr1
        unset $arr2
        unset $arr3
done

So I used unset to empty the array but it's giving error.所以我使用unset来清空数组,但它给出了错误。

line 49: unset: `x': not a valid identifier

I don't want to delete a particular member/index of array but entire array itself.我不想删除数组的特定成员/索引,而是删除整个数组本身。 Can anyone tell me how to do it?谁能告诉我怎么做?

unset works with variable names, not the values they keep. unset适用于变量名称,而不是它们保留的值。 So:所以:

unset arr1

or, if you want to empty it:或者,如果你想清空它:

arr1=()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM