Minor variable scope problem in hook scripts
feedback
Fri Jul 12 13:47:59 2019|
Hi Roy, I've discovered a minor problem during execution of dhcpcd hook scripts. I have 20-resolv.conf and 50-ntp.conf active and a generated ntp.conf file looks like this: # Generated by dhcpcd from # Generated by dhcpcd from eth0.dhcp, eth0.dhcp6, eth0.ra, eth0.static, eth0.dhcp, eth0.ra, eth0.static server XX.XX.XX.XX server XX.XX.XX.XX # End of dhcpcd from # Generated by dhcpcd from eth0.dhcp, eth0.dhcp6, eth0.ra, eth0.static, eth0.dhcp, eth0.ra, eth0.static I tracked this down to the 'header' variable in build_ntp_conf() which is not reset/unset before use so it still has the value from build_resolv_conf() where the same variable name is used and all variables are by default global if not declared otherwise. --- 50-ntp.conf-orig +++ 50-ntp.conf @@ -62,6 +62,7 @@ # Build a list of interfaces interfaces=$(list_interfaces "$ntp_conf_dir") + header= servers= if [ -n "$interfaces" ]; then # Build the header --- 20-resolv.conf-orig 2019-07-12 15:23:55.294195018 +0200 +++ 20-resolv.conf 2019-07-12 15:23:31.810088572 +0200 @@ -19,6 +19,7 @@ interfaces=$(list_interfaces "$resolv_conf_dir") # Build the resolv.conf + header= if [ -n "$interfaces" ]; then # Build the header for x in ${interfaces}; do Maybe we should check every variable in every function and if it is only used within that function declare it 'local' or use 'declare' itself (without option for strings) which would have the same effect. But this is just my bash knowledge, don't know how compatible these keywords are in sh and other shells. Kind regards, Andreas |
| Re: Minor variable scope problem in hook scripts | Roy Marples |