Register
It is currently Wed Apr 23, 2014 5:33 pm

Need command for a script... details inside.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Sep 18, 2006 2:46 pm   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
Greetings,

I'm sure a lot of people will be thinking I'm a total bash noob, well I'm still new but learning fast.

I need to find out how to find text in a text file and put a line of text i want AFTER the found line... example.


Let's say I have a file called list.txt and let's say there's a line in it "this is a line" and I want to put in "http://www.google.com/" (without quotes) AFTER "this is a line" (without quotes). How would I do that with bash so I can put it into a script?

I've seen a lot of replacing text within files but I don't want to "replace" the text, i want to "add text after" a particular line.

also while I'm here, is there a simple code to use that will make sure a script is run as root and exit with a sentence if not?

thank you.


Top
 Profile  
 PostPosted: Mon Sep 18, 2006 9:44 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
This is a pretty common way of checking for root.... use this function

Code:
checkforroot ()
{
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ]
then
   echo ""
   echo "**************** ERROR !! **************"
   echo "You must be logged in as root to run this script"
   echo "Please log in as root and re-run this script."
   echo "**************** ERROR !! **************"
   exit
fi
}


and call it with checkforroot


Quote:
I've seen a lot of replacing text within files but I don't want to "replace" the text, i want to "add text after" a particular line.


YES, you DO want to replace that line of text..... with 2 lines of text....... the current line (no change) and the 2nd line (the line you want to add)..... i'd use sed or ed for that.


Top
 Profile WWW  
 PostPosted: Tue Sep 19, 2006 4:29 am   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
thank you, that information is very useful to me :)


Top
 Profile  
 PostPosted: Tue Sep 19, 2006 12:26 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
Your welcome :) Glad I could help.


Top
 Profile WWW  
 PostPosted: Tue Sep 19, 2006 4:07 pm   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
Quote:
YES, you DO want to replace that line of text..... with 2 lines of text....... the current line (no change) and the 2nd line (the line you want to add)..... i'd use sed or ed for that.


one question, how exactly do i add 2 lines with sed? ive been trying to figure it out but have yet to figure it out.

also, not sure if im doing something wrong or what, but the checkforroot script doesnt work :( i tried it in a test script and i wasnt a root user and it let the script proceed.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP