I'm trying to temporarily disable dhcp on all connections in a computer using bash, so I need the process to be reversible. My approach is to comment out lines that contain BOOTPROTO=dhcp, and then insert a line below it with BOOTPROTO=none. I'm not sure of the correct syntax to make sed understand the line number stored in the $insertLine variable.
fileList=$(ls /etc/sysconfig/network-scripts | grep ^ifcfg) path="/etc/sysconfig/network-scripts/" for file in $fileList do echo "looking for dhcp entry in $file" if [ $(cat $path$file | grep ^BOOTPROTO=dhcp) ]; then echo "disabling dhcp in $file" editLine=$(grep -n ^BOOTPROTO=dhcp /$path$file | cut -d : -f 1 ) #comment out the original dhcp value sed -i "s/BOOTPROTO=dhcp/#BOOTPROTO=dhcp/g" $path$file #insert a line below it with value of none. ((insertLine=$editLine+1)) sed "$($insertLine)iBOOTPROTO=none" $path$file fi done Any help using sed or other stream editor greatly appreciated. I'm using RHEL 6.
sed -i "${insertLine}i BOOTPROTO=none" $path$filesed - i(edit in place) andNiwhereNis the number of the line to insert followed by the content to insert and finally what file to insert it in. You add${..}toinsertLineto protect the variable name from theithat follows and then the expression is double-quoted to allow variable expansion.