2

I know this has been answered several times, but I can't figure out how to use it for my project.

I have several php files and want to add a new string between line 11 and 12. this is the string I want to add

exec('/var/www/scripte/autostandby.sh > /dev/null 2>/dev/null &');

I guess this is the way to go (for a single file,for multiple I will use *.php instead of file):

sed '12i\
exec('/var/www/scripte/autostandby.sh > /dev/null 2>/dev/null &');
' file.php

what I get:

[1] 29234                                                                                                            
bash: $');\n': command not found  

so I guess bash just runs the exec command or something.

how do I add a "long" string containing spaces and stuff like ; ) , ' which seems to mess up the command?

p.s.: adding " " or ' ' doesn't trick the pony.

Bonus:

I also need to "append" something in line 11 from this:

exec('/var/www/streams/taketv.sh'); 

to this

exec('/var/www/streams/taketv.sh > /dev/null 2>/dev/null &'); 

If possible for all .php files. It's always line 11; the only thing that changes is "taketv.sh" to "abc.sh" or anything like that.

It is the first occasion that " '); " are shown in this combination, so I tried it with something like this (only for 1 file as a test):

 sed -i "s/');/ > /dev/null 2>/dev/null &');" TakeTV.php

I guess I need to use what mikeserv said. I tried but it didn't work.

sed -i 's\                                                                             
'\'');/'\''/'\'' > /dev/null 2>/dev/null &');'\''    

My plan was to set the "old word" and "new word" as a closed quote, guess I didn't apply it right!

2
  • while we are at it: i also need to append something in line 11 from this: exec('/var/www/streams/taketv.sh'); to this exec('/var/www/streams/taketv.sh > /dev/null 2>/dev/null &'); IF possible for all .php files. its always line 11, the only thing that changes is "taketv.sh" to "abc.sh" or anything like this. Nov 25 '15 at 9:52
  • You quoted too much! Basically you need to double up the quotes which are supposed to be quotes. So if you want a string to be this ain't your average string, you wrap it and double the inner quotes: 'this ain'\''t your average string'. see? when all of those quotes are removed from the string the ' through n is one quoted string, the following \' is one escaped apostrophe, and everything following that is the rest of the string. in any case, you don't have to do all of that work - you've already got most of what you need on line 11 - edit it, print it, edit it again. i made an edit.
    – mikeserv
    Nov 25 '15 at 13:46
1

You have to use several types of quotes or escape them:

sed -i "12iexec('/var/www/scripte/autostandby.sh > /dev/null 2>/dev/null &');" *.php      

Bonus:

 sed -i "s|exec('/var/www/streams/taketv.sh');|exec('/var/www/streams/taketv.sh > /dev/null 2>/dev/null \&');|" 
3
  • perfect, just had to remove the "/" directly infront of "exec" Nov 25 '15 at 10:19
  • i edited a little bonus to the question. would be really nice if you could help me. i didnt want to start a new question, because its pretty much the same topic. Nov 25 '15 at 11:33
  • I edit the answer for the bonus. Hope this help
    – djangoliv
    Nov 25 '15 at 13:06
0
sed '12i\
exec('\''/var/www/scripte/autostandby.sh > /dev/null 2>/dev/null &'\'');
' file.php

You have to quote the whole thing.

Your problem is you're trying to insert quotes from within a quoted string. It is doable, but you have to quote the quotes.

command 'begin quoted arg'\''
   close quotes
   backslash quote the apostrophe
   open quotes again
'\'' end quoted arg'

You can do without all of that though:

sed -e'11s|.)[^)]*$| >/dev/null 2>/dev/null&|p' \
    -e'11s|www.*sh|www/scripte/autostandby.sh|' <in >out

Still if you do use append or insert or change, it really is best to do so as you attempted to do in your question. Those commands are specified to require a newline separator between the commands and the text arguments they take.

2
  • 1
    i am not allowed to upvote yet, so i THANK YOU right here :) Nov 25 '15 at 14:15
  • @SchnitzelKopf - that will do just fine, and thanks for doing so.
    – mikeserv
    Nov 25 '15 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.