BASH - 循环错误

时间:2015-12-06 18:07:35

标签: bash loops while-loop

使用此代码我收到此错误,我的功能不可用:

  

Erreur desyntaxéprèsdusymbole inattendu«< »`done< <(echo -e   “$ TARGET_PORTS”)'警告:语法错误。或eval会导致   未来版本的shell将作为Posix需要中止

   function my_func{ 
    while read Port
    do
            CHECK_HG=`raidcom get host_grp -port $Port -IH${CCI_INST} |grep -iw ${HOST}`
            if [ ! -z "$CHECK_HG" ]; then
                    export HMO=`raidcom get host_grp -port $Port -IH${CCI_INST} | grep -i ${HOST} | awk -F " " '{print \$5}'`
                    if [ $HMO == "LINUX/IRIX" ]; then
                            HMO=`echo $HMO | awk -F "/" '{print \$1}'`
                    fi
                    #comment
                    NBPort=`expr $NBPort + 1`
                    #comment
                    PORT_HG=$PORT_HG"$Port;\n"
                    fi
    done < <(echo -e "$TARGET_PORTS")
}

因此,当我更改循环时,我没有收到任何错误,但我丢失了变量中的所有数据以在主脚本中恢复。

function my_func{
    echo -e "$TARGET_PORTS" | while read Port
    do
            CHECK_HG=`raidcom get host_grp -port $Port -IH${CCI_INST} |grep -iw ${HOST}`
            if [ ! -z "$CHECK_HG" ]; then
                    export HMO=`raidcom get host_grp -port $Port -IH${CCI_INST} | grep -i ${HOST} | awk -F " " '{print \$5}'`
                    if [ $HMO == "LINUX/IRIX" ]; then
                            HMO=`echo $HMO | awk -F "/" '{print \$1}'`
                    fi
                    #comment
                    NBPort=`expr $NBPort + 1`
                    #Comment
                    PORT_HG=$PORT_HG"$Port;\n"
                    fi
    done
}

有什么想法吗? 非常感谢。

1 个答案:

答案 0 :(得分:0)

您的重定向适用于bash脚本,其格式如https://stackoverflow.com/a/28927847/3220113所述 你可以在脚本的第一行添加一个shebang(#!/bin/bash),试图强制进入bash吗?

另一个解决方案(当您需要在AIX / Solaris和bash中的ksh中运行相同的脚本时很好)正在将while更改为for循环:

for Port in $(echo -e "$TARGET_PORTS") do
   ...
done