The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 01:55:38 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Sed isn't playing ball!  (Read 5089 times)
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« on: December 15, 2009, 05:46:21 PM »

Please can you help?  Need Help

I want to update putyourdbnamehere, usernamehere and  yourpasswordhere with values in $databaseName, $databaseUser and $databasePassword.  Then save the changes to wp-config.php.

I thought the code below would do this but it isn't and I can find a decent/simple resource on the net to help.

I'm running it with php shell_exec if that makes any difference.  Undecided

shell_exec('sed -e \'s/putyourdbnamehere/'.$databaseName.'/g\' -e \'s/usernamehere/'.$databaseUser.'/g\' -e \'s/yourpasswordhere/'.$databasePassword.'/g\' wp-config.php');

Cheers
TB
Logged
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #1 on: December 16, 2009, 05:01:46 AM »

I've just checked this by logging in via telnet and the sed command below worked as expected.  D'oh!

sed  -e 's/putyourdbnamehere/dbName/g' -e 's/usernamehere/dbUser/g' -e 's/dbpassword/dbPassword/g' wp-config.php

So I'm probably be doing something stupid, so I'll give it another go tonight.  Roll Eyes
Logged
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #2 on: December 16, 2009, 02:23:51 PM »

Still can't get it to work so I've done it in php instead.  Wink

I think I have the syntax wrong for updating the file using sed in a php shell_exec. 

If anyone could tell me the correct syntax I'd be grateful because I'd prefer to use sed then the php version.

Cheers
tb
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: December 16, 2009, 06:01:43 PM »

I'm surprised VSloathe hasn't checked in ... he's your best bet. Have not seen him around in a bit. Hang tough or send him a ping to wake him up, perhaps he'll drop by...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
walrus
Rookie
**
Offline Offline

Posts: 46


View Profile
« Reply #4 on: December 17, 2009, 07:21:47 AM »

Why not do it directly in php.. something like:

WARNING - CODE NOT TESTED...

$configFile = file_get_contents('wp-config.php');

$configFile = str_replace('putyourdbnamehere',$databaseName,$configFile);
$configFile = str_replace('usernamehere',$databaseUser,$configFile);
$configFile = str_replace('yourpasswordhere',$databasePassword,$configFile);

file_put_contents('wp-config.php',$configFile);
« Last Edit: December 17, 2009, 09:05:35 AM by walrus » Logged
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #5 on: December 17, 2009, 09:26:16 AM »

Why not do it directly in php.. something like:

WARNING - CODE NOT TESTED...

$configFile = file_get_contents('wp-config.php');

$configFile = str_replace('putyourdbnamehere',$databaseName,$configFile);
$configFile = str_replace('usernamehere',$databaseUser,$configFile);
$configFile = str_replace('yourpasswordhere',$databasePassword,$configFile);

file_put_contents('wp-config.php',$configFile);
Hi Walrus,

Cheers for the code.   

I've already done something similar to get my code working.  I wanted to use sed because I think it might be a lot quicker then doing it in php.
Logged
walrus
Rookie
**
Offline Offline

Posts: 46


View Profile
« Reply #6 on: December 17, 2009, 09:34:48 AM »

It might be faster with sed... but how many wp-configs do you have anyway  ? heheh...
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #7 on: December 17, 2009, 10:10:21 AM »

Sorry about not chiming in sooner.

I'd give you the advice to do it in code too. Not for performance reasons, but portability. You never know what kind of sed install is present on the system, which flags are supported, and if your test environment might be even a hair different than production, it's not worth the headache.

I do loves me some sed, though. Whatever string processing you can do, I don't care how advanced you want to get or what language you can use, I can do it all (with fewer keystrokes and processor cycles!) with sed, awk, find, and grep.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: December 17, 2009, 11:56:28 AM »

Thanks VS -

In which case, I'd take the code above and modify just a titch, if you're worried about speed:

file_put_contents('wp-config.php'
	
str_replace(
	
	
array(
'thenameofyourdb''yourusername''yourpassword'), 
	
	
array(
$databaseName$dbUser$dbPass), 
	
	
file_get_contents('wp-config.php')));

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #9 on: December 17, 2009, 12:50:10 PM »

Ok thanks everyone for the advice and the code. 

This mini project is now finished so it's straight onto the next one.  Roll Eyes ROFLMAO

Cheers
tb
Logged
walrus
Rookie
**
Offline Offline

Posts: 46


View Profile
« Reply #10 on: December 18, 2009, 07:20:47 AM »

I love this forum.

Thank you Perkiset, I would have never figured out that str_replace takes arrays (mixed parameter)..

Do you know how often I used multiple str_replace calls for multiple strings?

Back to coding...
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #11 on: December 18, 2009, 09:44:39 AM »

Cheers Walrus Smiley
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #12 on: December 18, 2009, 01:03:32 PM »

Tommy up there wrote me a PM, and in lieue of answering him directly I'm just going to post my response here in hopes that it's helpful to someone else, too.

First of all, I would try breaking it up into three separate shell_exec commands so that it's easier to debug. It could simply be a formatting error. More likely, though, is that PHP doesn't have the permissions to execute sed. Go to your path (probably /usr/bin/) and check the permissions on sed. It should at least be group-executable (775 works).

Also, remove the ticks around your sed expressions.
Logged

hai
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #13 on: December 18, 2009, 03:03:40 PM »

Cheers Vs, I pm'd you because I'm guessing people are getting bored of hearing me say 'it's still not working', so I thought solve the problem privately and then post what the problem + solution here.  But posting publicly is fine by me.   

Ok I've split the shell_exec commands into three, changed the script I'm running the sed command in (775) but I get the internal error below.   Shocked

Quote
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@mydomain and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at mydomain Port 80

So I set the script back to (644) and ran the following code:

Code:
echo shell_exec('echo putyourdbnamehere usernamehere | sed s/putyourdbnamehere/'.$databaseName.'/');
This worked as expected. 

Code:
shell_exec('sed s/putyourdbnamehere/newdbname/ wp-config.php');
This fails miserably. 

I then ran perks php solution to make sure it wasn't anything stupid like I was in the wrong directory or something, but the php worked fine.

As a result I'm guessing I've got the syntax for writing to a file wrong. But everywhere I've checked on the web says the syntax is right.

I've got a unix book ordered that will be arriving shortly, so hopefully that will help iron out any syntax errors.   Nerd
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #14 on: December 22, 2009, 09:27:17 AM »

Code:
shell_exec('sed s/putyourdbnamehere/newdbname/ wp-config.php');

It failed? The above shouldn't actually *do* anything, since you're not writing the stream, so I don't see how you could tell if it failed or not...

how bout instead:

Code:
shell_exec('cat wp-config.php | sed s/putyourdbnamehere/newdbname/ > wp-config.php');
Logged

hai
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!