LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-30-2008, 10:18 AM   #1
roadrash
LQ Newbie
 
Registered: Jan 2007
Location: Huntingdon, U.K.
Distribution: Ubuntu, Mint
Posts: 23

Rep: Reputation: 0
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?
 
Old 09-30-2008, 10:24 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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
 
Old 10-01-2008, 05:55 AM   #3
roadrash
LQ Newbie
 
Registered: Jan 2007
Location: Huntingdon, U.K.
Distribution: Ubuntu, Mint
Posts: 23

Original Poster
Rep: Reputation: 0
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

Last edited by roadrash; 10-01-2008 at 05:56 AM.
 
Old 10-01-2008, 06:32 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 10-01-2008, 09:03 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A package (or script) to echo "texttext" (append text) to a file on a ftp ? frenchn00b Programming 2 09-03-2008 04:27 PM
simple php script to add line/file to text file dnoy Programming 1 05-21-2008 05:08 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 05:28 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration