How to Update Text in File within Unix Shell Script

shellunix

I'm creating a shell script that needs to update the sudoers file. So basically I want to be able to append ":/usr/local/bin/" to the following text in /etc/sudoers:

secure_path = /sbin:/bin:/usr/sbin:/usr/bin

I need to use sudo in the command in order to gain the permissions to modify. Any idea how I can accomplish that?

Best Answer

So many ways!

The old standby is sed: sed 's/\(secure_path.*\)/\1:\/usr\/local\/bin/' /etc/sudoers

Scripts in vim can do advanced things as well. Any process you would type into vim can be saved to a vim script and played back.

The newest tool that's really handy for this is Augueas. It's often used with Puppet is structured around editing files in a structured manner. Thus, it's aware of context and hierarchies within a text file.