LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   append a line to text file in script (https://www.linuxquestions.org/questions/linux-newbie-8/append-a-line-to-text-file-in-script-673300/)

roadrash 09-30-2008 10:18 AM

append a line to text file in script
 
Hi I am modifying a script and I need a command to add a line to the end of the /etc/modules file. It currently reads like this

Quote:

fuse
lp
I need to get it to look like this

Quote:

fuse
lp
ath_pci
with a carriage return after ath_pci so it ends on a new line.

can someone help?

colucix 09-30-2008 10:24 AM

First do a backup of the original file somewhere, for example
Code:

cp -p /etc/modules /etc/modules.bck
then simply echo the string and append it to the end of the file:
Code:

echo ath_pci >> /etc/modules

roadrash 10-01-2008 05:55 AM

When I type sudo echo ath_pci >> /etc/modules I keep getting the message bash: /etc/modules: permission denied
I thought if I used sudo it should give me permission to do this

pixellany 10-01-2008 06:32 AM

I had thought that sudo was for enabling a user to run certain SW--but not to bypass permissions. (I never use it, so I'm not sure.)

Why not just "su" to get root powers? Especially when developing a script, you want to test things the way the script will run.

colucix 10-01-2008 09:03 AM

Using redirection with sudo should take some care. Your command line executes the echo command with root privileges then tries to store the output in /etc/modules as regular user. From the sudo man page there is an useful example:
Quote:

To make a usage listing of the directories in the /home partition. Note that this runs the commands
in a sub-shell to make the cd and file redirection work.

$ sudo sh -c "cd /home ; du -s * │ sort -rn > USAGE"
So you have to spawn the process in a subshell, using
Code:

sudo sh -c "echo ath_pci >> /etc/modules"


All times are GMT -5. The time now is 11:54 PM.