shell 字符串列表操作

        我的需求是这样的:编译多个模块的时候,把失败的模块添加到列表里,最后输出。这个列表类似这样:failModuleList="a b c d e"

        先定义一个空的字符串列表:

failModuleList=""

然后利用字符串的拼接方式把模块字符串添加入列表中:

function make_module_list()
{
    for modul in ${moduleList}
    do
        make -C ${modul} platform=$1 #函数里的 $1 是函数的入参,并非命令行传入的参数
        if [ $? -ne 0 ]; then
            failModuleList+=${modul}
            failModuleList+=" "
        fi
    done 

    echo -e "\033[31m\"${failModuleList}\" compile fail\033[0m"
}

列表(List):在Shell中,列表是由空格分隔的字符串序列。所以添加了空格,输出效果如下:

也可以用 for 的方式输出列表内容,如:

function make_module_list()
{
    for modul in ${moduleList}
    do
        make -C ${modul} platform=$1 #函数里的 $1 是函数的入参,并非命令行传入的参数
        if [ $? -ne 0 ]; then
            failModuleList+=${modul}
            failModuleList+=" "
        fi
    done 

    #echo -e "\033[31m\"${failModuleList}\" compile fail\033[0m"
    for module in ${failModuleList}
    do
        echo -e "\033[31m\"${module}\" compile fail\033[0m"
    done
}


 

注意:shell 的语法,“=”、“+=” 两边都不能有空格 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值