Jump to content

Syntax help


link7722

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/264226-syntax-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354073
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264226-syntax-help/#findComment-1354104
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.