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
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
Share on other sites

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.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.