0

I'm writing a bash script to generate the common file: /etc/udev/rules.d/70-persistent-net.rules. You can see my function has 3 arguments and 2 of them are arrays. I'm trying to print the array elements inline within the text below (where it says ATTR{address}== and save it to a file, but have not been successful.

I am also having problems preserving the quotation marks after the == arguments.

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
}

If your not familiar with the 70-persistent-net.rules file, I'm trying to make it look something like this using my arrays to print the mac addresses:

# 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"

Thank you

user1527227
  • 2,068
  • 5
  • 25
  • 36

1 Answers1

2

You should use a heredoc instead of echo with a multi-line string. This will prevent quoting issues as -- in your case -- " is used both as a string delimiter and inside the text.

    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
  • 50,096
  • 7
  • 103
  • 125
  • Thanks! This partially solved my problem. It seems that this code does not print the array to the file. How could I fix that? @Sylvain Leroux – user1527227 Jul 24 '14 at 17:00
  • @user1527227 I missed that part ... as it was dug at the end of your long string :D In fact, even if you are using a _heredoc_, you may redirect `cat` output as usual. I've edited my answer accordingly. – Sylvain Leroux Jul 24 '14 at 17:07
  • Thanks again @SylvainLeroux. Are here doc's the standard method of storing multiple lines of output to variables too? – user1527227 Jul 25 '14 at 20:26
  • @user1527227 _I_ use them all the time for that purpose. But to be honest, I don't know if this is the "standard" way of doing. Nor if it is the "recommended" way... – Sylvain Leroux Jul 25 '14 at 20:31