简体   繁体   English

Bash - 循环文件

[英]Bash - Looping Through the File

I am trying to run a simple loop through all files script but it's giving me a not spected result.我正在尝试通过所有文件脚本运行一个简单的循环,但它给了我一个意想不到的结果。

I'm using a user config file to loop content (example.conf).我正在使用用户配置文件来循环内容(example.conf)。

datasetA1 datasetB1 datasetC1
datasetA2 datasetB2 datasetC2
datasetA3 datasetB3 datasetC3
datasetA4 datasetB4 datasetC4

Script:脚本:

#!/bin/bash
    ramal=`cat /data/user/example.conf | awk '{ print $1 }'`
    name=`cat /data/user/example.conf | awk '{ print $2 }'`
    macid=`cat /data/user/example.conf | awk '{ print $3 }'`
    
    for ramais in ${ramal}; do
            for names in ${name}; do
                    for idmacs in ${macid}; do
                    echo "
                    [account]
                    path = /config/voip/sipAccount0.cfg
                    Enable = 1
                    Label = ${names}
                    DisplayName = ${names}
                    AuthName = ${ramais}
                    UserName = ${ramais}
                    password = ${ramais}
                    " > 001565${idmacs}.cfg
                    echo " "
                    echo "****   ${ramais} Nome ${names} Mac ${îdmacs} success provisioned ***** "
                    done
            done
    done

Result:结果:

001565datasetC1.cfg 001565datasetC1.cfg

        [account]
        path = /config/voip/sipAccount0.cfg
        Enable = 1
        Label = datasetB1
        DisplayName = datasetB1
        AuthName = datasetA1
        UserName = datasetA1
        password = datasetA1

001565datasetB1.cfg 001565数据集B1.cfg

        [account]
        path = /config/voip/sipAccount0.cfg
        Enable = 1
        Label = datasetB1
        DisplayName = datasetB1
        AuthName = datasetA1
        UserName = datasetA1
        password = datasetA1

Expected预期的

001565datasetC1.cfg 001565datasetC1.cfg

        [account]
        path = /config/voip/sipAccount0.cfg
        Enable = 1
        Label = datasetB1
        DisplayName = datasetB1
        AuthName = datasetA1
        UserName = datasetA1
        password = datasetA1

001565datasetC2.cfg 001565数据集C2.cfg

        [account]
        path = /config/voip/sipAccount0.cfg
        Enable = 1
        Label = datasetB2
        DisplayName = datasetB2
        AuthName = datasetA2
        UserName = datasetA2
        password = datasetA2

Nested loops give you the cartesian product.嵌套循环为您提供笛卡尔积。 You need just one loop that reads all fields in a line at once:您只需要一个循环来一次读取一行中的所有字段:

while read -r ramais names idmacs
    # insert you echo commands here
    echo "example: $ramais, $names, $idmacs"
done < /data/user/example.conf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM