将变量替换为存储在array - bash中的字符串

时间:2013-12-26 12:04:55

标签: bash unix

我的字符串存储在数组中,示例字符串包含“变量”。 迭代这个数组时,我想用'value'替换'variable'。

这对我失败了。我已经用各种方式尝试了很多方法。用Google搜索但无法弄清楚

# Array of strings (each string is a command)
clean_aa_commands=(
    "sourceanalyzer -b ${FortifyBuildId} -clean"    
    "cd ${unifiedbuilddir}/AA/AAUI"
    "mvn clean"
)  

# Functions  
function check {
    if [ "$?" -ne 0 ]; then
        echo "Operation [$1] Unsuccessful!"
    else
        echo "Operation [$1] Success!"  
    fi
    }

function runcmds {
        echo "using $FortifyBuildId *************************"
        cmdArr=("${!1}")
        for cmd in "${cmdArr[@]}"
        do
            echo "-->Running [$cmd]"
            eval "$cmd"
            check "$cmd"
            echo ""
            echo ""
        done
    }

   # main
   FortifyBuildId="$1"
   echo "FortifyBuildId is $FortifyBuildId"
   unifiedbuilddir=`pwd`
   runcmds clean_aa_commands["@"] 

1 个答案:

答案 0 :(得分:0)

这个怎么样

cmds='cmds=( /test/$mydir  /test/$test )'

mydir=$(pwd)
test="me"

eval ${cmds}
echo ${cmds[@]}