bash没有采取变量声明

时间:2016-06-23 08:04:58

标签: bash variables assignment-operator

我正在尝试使用bash函数内的引用将列表传递给变量,但是解释器将变量赋值作为命令读取。 请帮我理解我做错了什么。

我在等号之间寻找可能的空格但调试器(bash -x)没有显示空格

#!/bin/bash

parseNmerge () {
        for a in $( echo ${!2} )
        do
                str=$(echo $a|awk -F\# '{print $1"#"$2"#"$3"#"$4"#"$5"#"$6"#"$7"#"}')
                #declare "${1}"="$(echo "${!1}" |grep -v $str)"
                #declare "${1}"="$(echo -e "${!1}\n$a")"
                "${1}"="$(echo "${!1}" |grep -v $str)"
                "${1}"="$(echo -n "${!1}\n$a")"
        done
}

a_cmp=$(grep -v ^# param.cfg.2)
a_all=$(grep -v ^# param.cfg.1)

parseNmerge "a_all" "a_cmp"

for b in $a_all
do
        echo $b
done

运行脚本时,我收到以下抱怨:

$ ./xx
./xx: line 9: a_all=NAME#ALL#ALL#PROD#0#START#maximumpain#4
DEFAULT#TID#ALL#DEF#0#START#zarea#20000000: command not found

测试文件的内容是:

$ cat param.cfg.1

NAME#ALL#ALL#PROD#0#START#threadcount#1
NAME#ALL#ALL#PROD#0#START#maximumpain#4
\#NAME#ALL#ALL#DEVR#0#START#threadcount#7
DEFAULT#TID#ALL#DEF#0#START#zarea#20000000

$ cat param.cfg.2

NAME#ALL#ALL#PROD#0#START#threadcount#4
DEFAULT#TID#ALL#DEF#0#START#zarea#20000001

PS:当使用declare时,它可以正常使用函数内的declare使得结果集在函数外部不可用 PS:不使用引用时脚本运行正常 e.g:

#!/bin/bash

parseNmerge () {
        for a in $(echo $a_cmp)
        do
                str=$(echo $a|awk -F\# '{print $1"#"$2"#"$3"#"$4"#"$5"#"$6"#"$7"#"}')
                a_all=$(echo "$a_all"|grep -v "$str")
                a_all=$(echo -e "$a_all\n$a")
        done
}


a_cmp=$(grep -v ^# param.cfg.2)
a_all=$(grep -v ^# param.cfg.1)

parseNmerge

for b in $(echo $a_all)
do
        echo $b
done

0 个答案:

没有答案