【发布时间】: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