View previous topic :: View next topic |
Author |
Message |
Seron Apprentice
Joined: 31 Dec 2002 Posts: 293 Location: Malmö, Sweden
|
Posted: Mon May 24, 2010 1:40 am Post subject: [solved] run a command from within a bash script |
|
|
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 |
|
|
mikegpitt Advocate
Joined: 22 May 2004 Posts: 3224
|
Posted: Mon May 24, 2010 1:55 am Post subject: |
|
|
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 |
|
|
Seron Apprentice
Joined: 31 Dec 2002 Posts: 293 Location: Malmö, Sweden
|
Posted: Mon May 24, 2010 8:14 pm Post subject: |
|
|
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 |
|
|
mikegpitt Advocate
Joined: 22 May 2004 Posts: 3224
|
Posted: Tue May 25, 2010 1:25 am Post subject: |
|
|
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 |
|
|
Seron Apprentice
Joined: 31 Dec 2002 Posts: 293 Location: Malmö, Sweden
|
Posted: Tue May 25, 2010 5:35 am Post subject: |
|
|
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 |
|
|
Seron Apprentice
Joined: 31 Dec 2002 Posts: 293 Location: Malmö, Sweden
|
Posted: Tue May 25, 2010 7:16 pm Post subject: |
|
|
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 |
|
|
|