The other day I needed to create a RightScript shell script that would update a couple configuration files on a server that was being launched in the RackSpace Cloud via RightScale. I decided to use SED to find and replace content within the configuration files. The first pass at the script failed because what I thought were spaces ended up being tabs. Use the information below to represent a tab within a shell script when using sed.
First RightScript Attempt Using Spaces:
The example below was the first attempt at attempting to replace a configuration line within the sudoers file located in the /etc directory on CentOS Linux. I needed to allow the wheel group sudo access by uncommenting the default “# %wheel ALL=(ALL) ALL” configuration line. Below is the first attempt at doing this.
Use sed To Uncomment Line In sudoers File:
- # modify sudoers to allow members of wheel group to sudo
- /bin/sed -ie "s/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/g" /etc/sudoers
Second RightScript Attempt Using Tabs:
This did not work because instead of 6 spaces as originally thought the configuration file actually includes tabs. You can represent a tab when using sed’s search and replace with a backslash t or \t. The below example shows the RightScript that failed above is fixed below using a \t for each location where a tab exists in the sudoers configuration file line being replaced.
Use sed With \t For Tabs To Uncomment Line In sudoers File:
- # modify sudoers to allow members of wheel group to sudo
- /bin/sed -ie "s/# %wheel\\tALL=(ALL)\\tALL/%wheel\\tALL=(ALL)\\tALL/g" /etc/sudoers
After updating the RightScript that is basically just a shell script and launching a CentOS Linux RackSpace node or instance via RightScale that uses the RightScript on boot of this style node the sudoers file was updated as expected.