shell 使用来自命令替换的两个变量进行循环迭代的Bash

nbnkbykc  于 2023-01-31  发布在  Shell
关注(0)|答案(2)|浏览(163)

我尝试在bash脚本中执行一个for循环,需要传递两个变量。我目前正在使用下面的for循环,但没有成功。

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

因此,我想使用命令替换来使用componentId和packageVersion变量迭代上述for循环,其中${componentIds}和${packageVersions}的值已经设置为env变量。因此,我只是尝试回显该命令并获得输出。这些变量的输出值由分隔,因此IFS =','
当我执行我当前的脚本时,它抱怨缺少参数packageVersion。
任何解决这个问题的建议都会很有帮助,谢谢!!

8fsztsew

8fsztsew1#

您可以采取两种方法:

  • 对两个参数中的每一个进行循环(如果分别管理这些参数),或者
  • 在数组中定义的一组匹配参数上循环。

这两种方法表示为以下logic_01和logic_02:

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
}

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
}
6ss1mwsb

6ss1mwsb2#

下面的Shellcheck-clean代码演示了一种同时循环两个逗号分隔列表的方法:

#! /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

相关问题