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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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 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.
Thanks again @SylvainLeroux. Are here doc's the standard method of storing multiple lines of output to variables too?
@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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.