Click to See Complete Forum and Search --> : Single Perl Command script


tiggerific
05-01-2005, 10:34 AM
Hi guys,

I'm being kind of lazy here... I'm not a Perl programmer and don't wish to be (I'm working on PHP at the moment) and I have a one-line command that I can run from SSH that I'd like to be able to run as a Perl script (specifically for my clients that don't have SSH enabled).

Could someone please give me the additional code I would need to make this command line work in an executable Perl script (which does run fine from bash)?

find ./*/*.php -type f -exec sed -i 's/currentip/newip/' {} \;

I've tried various combinations of things including with and without single and double quotes, using different relative path locations, etc. but I'm always getting some type of error. The command simply goes through all PHP files in all subdirectories and replaces "currentip" with "newip".

Hard-coding the currentip and newip is not a problem (i.e. I don't really need variables for them).

Any help will be appreciated.

Thanks

tiggerific
05-13-2005, 09:11 AM
Couldn't get any experts to give me a hint with this one?
Surely someone knows how to get this working from a CGI script.

jimr451
05-13-2005, 02:44 PM
Hi,

I'll take a crack, though I haven't tested it:

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

open(IN,"find ./*/*.php -type f -exec sed -i 's/currentip/newip/' {} \; |");
while(<IN>) {
#Shows the output of the command
print;
}
close IN;

-------------

Note - if you want "current_ip" and "newip" to be dynamic values, you can pass those in on a GET string or from a form. I can give you that variant as well, I just wasn't sure where those values came from.

Also, note you might need to add the full paths to "find" and the starting directory, depending on teh system and where the perl script runs.

Also, note this isn't the best way to go about this from a Perl perspective, but I think it should work!

Hope this helps,

-Jim

tiggerific
05-13-2005, 08:29 PM
Ok thanks for your reply - i'll give it a shot. I don't actually need dynamic variables as it's usually only a one-time run on a particular site after a server change. But I've had a few sites running the same script require a change in all files in the script.

As I said, if they have shell access - it's not a problem to run it from the command line as it only needs to be done once (maybe twice). And I think the key ingredient I may have been missing was the location of the actual FIND command (didn't think of that).

I'll let you know how it goes.