How to add a line to a file which has only root write permission and to continue the script execution
There's a tip in the sudo
man page which explains how to do something like this. Here's my one-liner:
#!/usr/bin/bash
sudo sh -c "echo \"add this line to the code\" >> fileName"
Obviously, you'll first have to set up your user to have sudo
privileges. The sh
shell is used because of the redirection to the root-owned file. I also had to escape the quotes used for the echo
command.
su
is available on most unix systems and should work:
su root -c 'echo "add this line to the code" >> fileName'