Home > Mobile >  Bash for loop iteration with two variables from command substitution
Bash for loop iteration with two variables from command substitution

Time:01-31

I'm trying to execute a for loop in bash script where I have the requirement to pass two variables. I'm currently using the below for loop without success.

IFS=',' ;for componentId in `echo "${componentIds}"`, packageVersion in `echo "${packageVersions}"`; 
do 
    notes="${saveNotes}"
    deployNotes="${saveNotes}"
packageVersion=`echo "${packageVersion}" | xargs`
savePackageVersion="${packageVersion}"
componentId=`echo "${componentId}" | xargs`
saveComponentId="${componentId}"
    componentType="${saveComponentType}"
    listenerStatus="${saveListenerStatus}"
    envId=${saveEnvId}
    source bin/createSinglePackage.sh componentId=${componentId} componentType="${componentType}" packageVersion="${packageVersion}" notes="${notes}" extractComponentXmlFolder="${extractComponentXmlFolder}" componentVersion=""
done   

So I want to iterate the above for loop with both componentId and packageVersion variables using command substitution where the values of ${componentIds} and ${packageVersions} are already set as env variables. So I'm just trying to echo that and get the output. The output values of these variables are delimited by , and hence the IFS=','

When i execute my current script it complains about missing parameter packageVersion.

Any suggestions to address this would be quite helpful, thank you!!

CodePudding user response:

You have two approaches you can take:

  • loop on each of the two parameters (if those are managed separately), or
  • loop on a matched set of parameters defined in an array.

Those two approaches are presented as logic_01 and logic_02 below:

logic_1()
{
IFS=',' 
for componentId in `echo "${componentIds}" `
do
    for packageVersion in `echo "${packageVersions}" `
    do
        notes="${saveNotes}"
        deployNotes="${saveNotes}"
        packageVersion=`echo "${packageVersion}" | xargs`
        savePackageVersion="${packageVersion}"
        componentId=`echo "${componentId}" | xargs`
        saveComponentId="${componentId}"
        componentType="${saveComponentType}"
        listenerStatus="${saveListenerStatus}"
        envId=${saveEnvId}

        source bin/createSinglePackage.sh \
            componentId=${componentId} \
            componentType="${componentType}" \
            packageVersion="${packageVersion}" \
            notes="${notes}" \
            extractComponentXmlFolder="${extractComponentXmlFolder}" \
            componentVersion=""
    done
done
}

... OR ...

logic_2()
{
packages=( "comp01:vers01" "comp01:vers02" "comp02:vers05" "comp02:vers06" "comp03:vers02" "comp03:vers04" )

for packages in ${packages[@]}
do
    componentId=`echo "${packages}" | cut -f1 -d\: `
    packageVersion=`echo "${packages}" | cut -f2 -d\: `

    notes="${saveNotes}"
    deployNotes="${saveNotes}"
    packageVersion=`echo "${packageVersion}" | xargs`
    savePackageVersion="${packageVersion}"
    componentId=`echo "${componentId}" | xargs`
    saveComponentId="${componentId}"
    componentType="${saveComponentType}"
    listenerStatus="${saveListenerStatus}"
    envId=${saveEnvId}

    source bin/createSinglePackage.sh \
        componentId=${componentId} \
        componentType="${componentType}" \
        packageVersion="${packageVersion}" \
        notes="${notes}" \
        extractComponentXmlFolder="${extractComponentXmlFolder}" \
        componentVersion=""
done
}

CodePudding user response:

This Shellcheck-clean code illustrates one way to loop through two comma-separated lists at the same time:

#! /bin/bash -p

componentIds=compid1,compid2,compid3,compid4,compid5
packageVersions=pkgver1,pkgver2,pkgver3,pkgver4,pkgver5

IFS=, read -r -a compids <<<"$componentIds"
IFS=, read -r -a pkgvers <<<"$packageVersions"

for i in "${!compids[@]}"; do
    componentId=${compids[i]}
    packageVersion=${pkgvers[i]}
    printf 'componentId=%q, packageVersion=%q\n' "$componentId" "$packageVersion"
done
  • Related