Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved] run a command from within a bash script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
Seron
Apprentice
Apprentice


Joined: 31 Dec 2002
Posts: 293
Location: Malmö, Sweden

PostPosted: Mon May 24, 2010 1:40 am    Post subject: [solved] run a command from within a bash script Reply with quote

I'm trying to add some text to a file after a line with the text "some_line_in_the_file" by means of a shell script.

Here's the relevant part:
Code:
new_text="line1
line2
line3"

eval 'sed "/some_line_in_the_file/ a\ $new_text" /path/to/file'


When I run this script I get the error
Code:
sed: -e expression #1, char 69: unknown command: `k'


What is wrong with this code?
_________________
man cannot be brave without being afraid


Last edited by Seron on Mon May 24, 2010 8:15 pm; edited 1 time in total
Back to top
View user's profile Send private message
mikegpitt
Advocate
Advocate


Joined: 22 May 2004
Posts: 3224

PostPosted: Mon May 24, 2010 1:55 am    Post subject: Reply with quote

Try this instead:
Code:
new_text="line1
line2
line3"

sed "/some_line_in_the_file/ a\ `echo $new_text`" /path/to/file'
Back to top
View user's profile Send private message
Seron
Apprentice
Apprentice


Joined: 31 Dec 2002
Posts: 293
Location: Malmö, Sweden

PostPosted: Mon May 24, 2010 8:14 pm    Post subject: Reply with quote

Thanks. I tried it but it didn't work. I solved it with perl instead.
Code:
perl -pe "s#(some_line_in_the_file.+)#\1\n\n${line1}\n${line2}\n${line3}#" < "$src" > "$dst"

_________________
man cannot be brave without being afraid
Back to top
View user's profile Send private message
mikegpitt
Advocate
Advocate


Joined: 22 May 2004
Posts: 3224

PostPosted: Tue May 25, 2010 1:25 am    Post subject: Reply with quote

Seron wrote:
Thanks. I tried it but it didn't work. I solved it with perl instead.
Code:
perl -pe "s#(some_line_in_the_file.+)#\1\n\n${line1}\n${line2}\n${line3}#" < "$src" > "$dst"
Hmm... not sure why it didn't work... I tested it on my system and seemed to run fine. Either way, glad to hear you got things working :)
Back to top
View user's profile Send private message
Seron
Apprentice
Apprentice


Joined: 31 Dec 2002
Posts: 293
Location: Malmö, Sweden

PostPosted: Tue May 25, 2010 5:35 am    Post subject: Reply with quote

My script is supposed to generate a new entry in grub.conf based on the previous top entry. If grub.conf has this top entry it would choke on line 3.
Code:
title=Gentoo Linux 2.6.32-r7-ksm
root (hd0,0)
kernel /boot/kernel-2.6.32-gentoo-r7-ksm root=/dev/hda3 video=1024x768@60

The perl approach didn't solve it completely, which I didn't notice at first. It silently failed at the @ character which I had to escape twice before handing the line to perl, like so.
Code:
line3=$(echo $line3 | sed -r -e "s#@#\\\@#g")

_________________
man cannot be brave without being afraid
Back to top
View user's profile Send private message
Seron
Apprentice
Apprentice


Joined: 31 Dec 2002
Posts: 293
Location: Malmö, Sweden

PostPosted: Tue May 25, 2010 7:16 pm    Post subject: Reply with quote

I got it working with sed. Almost exactly what you suggested. I didn't analyse why it failed before. Just read up on bash and sed. This also eliminated having to escape '@' for perl. Thanks for your help.
Code:
sed '/some_line_in_the_file/ a\'"$new_text" "$path_file"

_________________
man cannot be brave without being afraid
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum