Using sudo with a redirect

I am trying to add a line to the /etc/rc.conf file via a script (so I can recreate it automatically) which requires using sudo (I do not have the root's password). I get a permissions error when I try sudo echo 'apache22_enable="YES"' >> /etc/rc.conf and I believe that sudo is handling the echo 'apache22_enable="YES"' but is not handling the >> /etc/rc.conf portion.

How do I get sudo to apply the echo to the file?

Here are a couple of things I have tried:
Code:
$ sudo echo 'apache22_enable="YES"' >> /etc/rc.conf
cannot create /etc/rc.conf: Permission denied
$
$ sudo -c "echo 'apache22_enable="YES"' >> /etc/rc.conf"
sudo: unknown login class: echo 'apache22_enable=YES' >> /etc/rc.conf
sudo: unable to initialize policy plugin
$
$ sudo -s "echo 'apache22_enable="YES"' >> /etc/rc.conf"
#
# exit
$

Since I don't have the root password I cannot run su -C "[i]some command here[/i]"
 
Code:
# sudo su -
# echo 'apache22_enable="YES"' >> /etc/rc.conf
Even safer:
Code:
# sudoedit /etc/rc.conf
Then you can just copy/paste or even type the info in.
 
I ran this command
phoenix said:
Code:
# sudo su -
# echo 'apache22_enable="YES"' >> /etc/rc.conf

And I got this entered into the file.
Code:
apache22_enable="YES"
It matches the other flags already present in the rc.conf file.

Then I tried this command
derekschrock said:
sudo sh -c "echo 'apache22_enable="YES"' >> /etc/rc.conf"
It worked, but the " and ' must have confused things a bit because what was entered into the file did not include the " around "YES".
Code:
apache22_enable=YES

I do prefer to second way, as it doesn't drop me into full-blown-root, but I don't know what the effect of the missing "" could produce.
 
Back
Top