【问题标题】:print array elements within text body in bash在bash中的文本正文中打印数组元素
【发布时间】:2014-09-16 07:56:10
【问题描述】:

我正在编写一个 bash 脚本来生成公共文件:/etc/udev/rules.d/70-persistent-net.rules。你可以看到我的函数有 3 个参数,其中 2 个是数组。我正在尝试在下面的文本中内联打印数组元素(其中显示 ATTR{address}== 并将其保存到文件中,但没有成功。

我在保留 == 参数后的引号时也遇到了问题。

function make_70_persistent_net_rules_file() {
# argument 1:  intel_mac_number     - number                                                                                                                                                    
# argument 2:  intel_mac_addresses  - array with 2 or 4 elements                                                                                                                                
# argument 3:  im_mac_addresses     - array with 2 elements                                                                                                                                     

    if [ "$intel_mac_number" -eq "2" ]; then
        echo "# This file was automatically generated by the /lib/udev/write_net_rules                                                                                                          
# program, run by the persistent-net-generator.rules rules file.                                                                                                                                
#                                                                                                                                                                                               
# You can modify it, as long as you keep each rule on a single                                                                                                                                  
# line, and change only the value of the NAME= key.                                                                                                                                             

# PCI device 0x8086:0x1521 (igb)                                                                                                                                                                
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${intel_mac_addresses[0]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"                                                          

# PCI device 0x8086:0x1521 (igb)                                                                                                                                                                
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${intel_mac_addresses[1]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"                                   

# PCI device 0x8086:0x10e6 (igb)                                                                                                                                                                
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${im_mac_addresses[0]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"                                      

# PCI device 0x8086:0x10e6 (igb)                                                                                                                                                                
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${im_mac_addresses[1]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth3""  > 70-persistent-net.rules-TEST
    fi
}

如果您不熟悉 70-persistent-net.rules 文件,我正在尝试使用我的数组打印 MAC 地址使其看起来像这样:

# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x10ec:0x8168 (r8169)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="bb:bb:bb:bb:bb:bb", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x0887 (iwlwifi)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="aa:aa:aa:aa:aa:aa", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

谢谢

【问题讨论】:

    标签: arrays bash printing echo heredoc


    【解决方案1】:

    您应该使用heredoc 而不是echo 与多行字符串。这将防止引用问题 - 在您的情况下 - " 既用作字符串分隔符,又用作文本内部

        if [ "$intel_mac_number" -eq "2" ]; then
            cat > 70-persistent-net.rules-TEST << EOF
    # This file was automatically generated by the /lib/udev/write_net_rules                                                                                                          
    # program, run by the persistent-net-generator.rules rules file.                                                                                                                                
    #                                                                                                                                                                                               
    # You can modify it, as long as you keep each rule on a single                                                                                                                                  
    # line, and change only the value of the NAME= key.                                                                                                                                             
    
    # PCI device 0x8086:0x1521 (igb)                                                                                                                                                                
    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${intel_mac_addresses[0]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"                                                          
    
    # PCI device 0x8086:0x1521 (igb)                                                                                                                                                                
    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${intel_mac_addresses[1]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"                                   
    
    # PCI device 0x8086:0x10e6 (igb)                                                                                                                                                                
    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${im_mac_addresses[0]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"                                      
    
    # PCI device 0x8086:0x10e6 (igb)                                                                                                                                                                
    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${im_mac_addresses[1]}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth3"
    EOF
        fi
    

    【讨论】:

    • 谢谢!这部分解决了我的问题。似乎此代码不会将数组打印到文件中。我怎么能解决这个问题? @Sylvain Leroux
    • @user1527227 我错过了那部分......因为它是在你的长字符串的末尾挖出来的 :D 事实上,即使你使用的是 heredoc,你也可以像往常一样重定向cat 输出。我已经相应地编辑了我的答案。
    • 再次感谢@SylvainLeroux。这里的文档也是将多行输出存储到变量的标准方法吗?
    • @user1527227 为此目的一直使用它们。但老实说,我不知道这是否是“标准”的做法。也不是“推荐”的方式......
    猜你喜欢
    • 2016-08-25
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多