link7722 Posted June 15, 2012 Share Posted June 15, 2012 I run the following command in Linux shell and get the expected result: sed -i 's/domain = example.com/domain = whatever.com/" /etc/test/test.conf The sed command searches the file "etc/test/tect.conf" and finds the string "domain = example.com" which then replaces with "domain = whatever.com". Now I am trying to run the command from php where the string "example.com" is a variable and "whatever.com" is another one.I am using the shell_exec() function but I think that there is something wrong with the syntax-below is the syntax: shell_exec('sed -i ' . $q . 's/auth_default_realm = '. $olddomain . '/domain = '. $newdomain . '/' . $q . '/etc/test/test.conf'); $q = "'" ; (single quote) Thank you Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/ Share on other sites More sharing options...
trq Posted June 15, 2012 Share Posted June 15, 2012 Firstly, the command you posted will fail because you are mixing single and double quotes. Secondly, your making your php example more complicated than it need be: shell_exec("sed -i 's/auth_default_realm = $olddomain/domain = $newdomain/' /etc/test/test.conf"); Be sure to validate and escape $olddomain and $newdomain especially if they are coming from user input. Your opening yourself to massive security concerns otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354073 Share on other sites More sharing options...
link7722 Posted June 15, 2012 Author Share Posted June 15, 2012 Thank you for your answer. Yes I mistyped the linux command in the post. Your suggestion throws the following error (in apache error log): sed: -e expression #1, char 31: unterminated `s' command Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354079 Share on other sites More sharing options...
trq Posted June 15, 2012 Share Posted June 15, 2012 Works fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354084 Share on other sites More sharing options...
link7722 Posted June 15, 2012 Author Share Posted June 15, 2012 That way aren't the 2 variables ($olddomain-$newdomain) treated as strings ? Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354087 Share on other sites More sharing options...
link7722 Posted June 15, 2012 Author Share Posted June 15, 2012 Yes . Your suggestion runs OK if I replace the variables with strings, but with the variables inside the quotes of the sed command it will not recognize them. Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354095 Share on other sites More sharing options...
trq Posted June 15, 2012 Share Posted June 15, 2012 I'm really not sure what your talking about. Are you sure you want to replace auth_default_realm ? Maybe: shell_exec("sed -i 's/domain = $olddomain/domain = $newdomain/' /etc/test/test.conf"); is what your really trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354097 Share on other sites More sharing options...
link7722 Posted June 15, 2012 Author Share Posted June 15, 2012 I want to replace "auth_default_realm = domain1" with "auth_default_realm = domain2". The following command runs OK from the Linux shell: sed -i 's/auth_default_realm = domain1/auth_default_realm = domain2/' /etc/test/test.conf Now from PHP with shell_exec() and replacing domain1 and domain2 with 2 variables, the following command is not OK shell_exec("sed -i 's/auth_default_realm = $olddomain/auth_default_realm = $newdomain/' /etc/test/test.conf"); I see that the 2 variables ($olddomain and $newdomain) are enclosed in quotes and they are not passed correctly with shell_exec. Thank you and sorry for the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354104 Share on other sites More sharing options...
trq Posted June 15, 2012 Share Posted June 15, 2012 shell_exec("sed -i 's/auth_default_realm = $olddomain/auth_default_realm = $newdomain/' /etc/test/test.conf"); Works as expected for me. Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354105 Share on other sites More sharing options...
link7722 Posted June 15, 2012 Author Share Posted June 15, 2012 Can you please check the following one also.Is that also running OK? (It has the same result as the previous.) shell_exec('sed -i "s/auth_default_realm.*/auth_default_realm = $newdomain/" /etc/dovecot/dovecot.conf'); Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354108 Share on other sites More sharing options...
trq Posted June 15, 2012 Share Posted June 15, 2012 No that will not work. Variables are not interpolated within single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354110 Share on other sites More sharing options...
link7722 Posted June 15, 2012 Author Share Posted June 15, 2012 OK.You are right.Working now Thank you Quote Link to comment https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.