How to copy an array in Bash?

后端 未结 8 1919
陌清茗
陌清茗 2020-12-02 15:31

I have an array of applications, initialized like this:

depends=$(cat ~/Depends.txt)

When I try to parse the list and copy it to a new arra

相关标签:
8条回答
  • 2020-12-02 15:53

    I've discovered what was wrong.. My if isn't installed test is two for loops that remove excess characters from file names, and spits them out if they exist on a certain web server. What it wasn't doing was removing a trailing hyphen. So, when it tested it online for availability, they were parsed out. Because "file" exists, but "file-" doesn't.

    0 讨论(0)
  • 2020-12-02 16:02

    Problem is to copy array in function to be visible in parent code. This solution works for indexed arrays and if before copying are predefined as declare -A ARRAY, works also for associative arrays.

    function array_copy
    # $1 original array name
    # $2 new array name with the same content
    {
        local INDEX
        eval "
            for INDEX in \"\${!$1[@]}\"
            do
                $2[\"\$INDEX\"]=\"\${$1[\$INDEX]}\"
            done
        "
    }
    
    0 讨论(0)
提交回复
热议问题