Sample Header Ad - 728x90

Associative Array contains filenames and paths

-2 votes
1 answer
79 views
I have a pipeline that deploys scripts into a temporary dir. I need to rellocate them into their actual proper directory.
#!/usr/bin/sh

DEPLOY_HOME=/opt/xxx

function getDeployedFiles {
    ls ${DEPLOY_HOME} | grep -v pipeline.properties | grep -v Deploy.tmp > 
    ${DEPLOY_HOME}/Deploy.tmp
    chmod 700 ${DEPLOY_HOME}/Deploy.tmp
    readarray -t depFilesArr < ${DEPLOY_HOME}/Deploy.tmp
}

function setDict {
    declare -A dict
    dict=(
        ["a.script.100"]="/opt/xxx/firstpath/100"
        ["b.script.200"]="/opt/xxx/secondpath/200"
        ["c.script.300"]="/opt/xxx/thirdpath/300"
    )
}

function itArr {
    for i in "${depFilesArr[@]}"; do
        FILENAME="${i}"
        if [[ -v "${dict['${i}']}" ]]; then
            echo "Value is present"
            FILEDEST="${dict['${i}']}"
            mv ${DEPLOY_HOME}/${FILENAME} ${FILEDEST}/${FILENAME}
        else
            echo "Value not found, file will remain in ${DEPLOY_HOME}"
        fi
    done
}

#MAIN PROGRAM
getDeployedFiles
setDict 
itArr
The current error I am getting is: "script.sh: line x 'a.script.100': syntax error: operand expected (error token is "'a.script.100'") It doesn't seem to evaluate the "if" properly as it's not printing any of the subsequent echos. Idk wether I'm incorrectly managing the strings or something about my logic is wrong, any help appreciated. BTW I'm using bash 4.4.
Asked by KrOo Pine (107 rep)
Oct 31, 2024, 07:27 PM
Last activity: Nov 1, 2024, 03:13 PM