LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2007, 11:43 AM   #1
khairil
LQ Newbie
 
Registered: May 2005
Distribution: gentoo
Posts: 23

Rep: Reputation: 15
bash: append string to end of line


i'm writing a patch script which including to append 'reboot=b' to grub kernel parameter to my linux box (many of them)

here's what i wrote;
Code:
#!/bin/bash
...
# append reboot=b to kernel parameter - 2007-02-26
kernel=`sed -n '/^kernel/p' /boot/grub/menu.lst | sed q` #get only the first kernel title
kernelappended="$kernel reboot=b"
if [ x$(echo $kernel | grep reboot=b) = "x" ]; then 
	#cp --parents /boot/grub/menu.lst $backupdir # ignore this line
	sed "s/$kernel/$kernelappended" /boot/grub/menu.lst
fi
...
example;
the original: kernel /kernel-2.6.19-gentoo-r5 root=/dev/hda3 video=vesafb:ywrap,mtrr,800x600-16@60 splash=silent,fadein,theme:walpurgisnacht quiet CONSOLE=/dev/tty1

will become: the original: kernel /kernel-2.6.19-gentoo-r5 root=/dev/hda3 video=vesafb:ywrap,mtrr,800x600-16@60 splash=silent,fadein,theme:walpurgisnacht quiet CONSOLE=/dev/tty1 reboot=b

however the substitute sed command (sed "s/$kernel/$kernelappended" /boot/grub/menu.lst) failed..
please help me..

tq

Last edited by khairil; 02-26-2007 at 11:45 AM.
 
Old 02-26-2007, 12:17 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Substitute the strings manually in the sed command, and you will see what's happening:

Code:
sed "s/kernel /kernel-2.6.19-gentoo-r5 root=/dev/hda3 video=vesafb:ywrap,mtrr,800x600-16@60...
You've got a few extra '/' in there. You need to use a delimiter that won't appear in the string, or escape each occurrence of the delimiter in the string.
 
Old 02-26-2007, 12:22 PM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Code:
sed "s/$kernel/$kernelappended" /boot/grub/menu.lst
My awfull way of doing it:
Code:
export xkernel=$kernel
export xkernelappended=$xkernelappended
sed -e `echo "s/$xkernel/$xkernelappended/"` /boot/grub/menu.lst
export xkernel=
export xkernelappended=
edit:
yeah, true for what macemoneta says.

Last edited by nx5000; 02-26-2007 at 12:23 PM.
 
Old 02-26-2007, 12:26 PM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Less horrible:

Code:
sed -e "s/"$kernel"/"$kernelappended"/" /boot/grub/menu.lst
 
Old 02-26-2007, 12:40 PM   #5
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
So to integrate previous comments, you would have to replace / by \/ before doing the sed
Code:
kernel=${kernel//\//\\/}
kernelappended=${kernelappended//\//\\/}
sed -e "s/"$kernel"/"$kernelappended"/" /boot/grub/menu.lst
or in one go

Code:
sed -e "s/"${kernel//\//\\/}"/"${kernelappended//\//\\/}"/" /boot/grub/menu.lst
Hum not really readable anymore..
 
Old 02-26-2007, 02:57 PM   #6
jr1
LQ Newbie
 
Registered: Feb 2007
Distribution: mostly Debian
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by nx5000
Code:
sed -e "s/"${kernel//\//\\/}"/"${kernelappended//\//\\/}"/" /boot/grub/menu.lst
Hum not really readable anymore..
You can increase readability by using a delimiter that's not in either string. Like this
Code:
sed "s:/foo:/bar:"     # readable
sed "s/\/foo/\/bar/"   # not so readable
Since the strings in your example probably have colons (":") in them, you'll want a different delimiter. Maybe "^",
Code:
sed "s^$kernel^$kernelappended^" /boot/grub/menu.lst

Last edited by jr1; 02-26-2007 at 03:03 PM.
 
Old 02-27-2007, 05:09 AM   #7
khairil
LQ Newbie
 
Registered: May 2005
Distribution: gentoo
Posts: 23

Original Poster
Rep: Reputation: 15
thx to all, i didn't know that i can replace any character as delimiter for sed.. kewl!
 
  


Reply



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
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
How can I check if there slash in the end of string in bash if? nadavvin Programming 5 01-27-2007 02:41 AM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration