PDA

View Full Version : Maximum length for commands


Wassercrats
07-23-2005, 07:56 AM
Here's the weekend edition of my column.

This command doesn't find and replace what it should:find * -name '*.shtml' | xargs perl -pi -e "s/\<body\>.*\<\!\-\-\#include virtual\=\"\/cgi\-bin\/Banner_BackgroundNotes\.pl\" \-\-\>\<br\>/\<body style = \"margin\-top: 1px\; padding\-top: 1px\;\"\>\n\n\<p style = \"text\-align: center\; margin\-top: 1px\; padding\-top: 1px\;\"\>\n\<a href = \"http:\/\/www\.unicef\.org\"\>\n\<img style = \"margin\-bottom: 20px\; border: none\;\" src = \"http:\/\/www\.polisource\.com\/images\/Banner_BackgroundNotes\.gif\"\>\n\<\/a\>\n\<\/p\>\n\n\<br\>/s;"I tested the regex on my home PC with the following code, and it worked:$test = "<body>\n\n<!--#include virtual=\"/cgi-bin/Banner_BackgroundNotes.pl\" --><br>\n<b><i>Bureau of Western Hemisphere";

print "-------------------$test--------------------\n\n\n";

$test =~ s/\<body\>.*\<\!\-\-\#include virtual\=\"\/cgi\-bin\/Banner_BackgroundNotes\.pl\" \-\-\>\<br\>/\<body style = \"margin\-top: 1px\; padding\-top: 1px\;\"\>\n\n\<p style = \"text\-align: center\; margin\-top: 1px\; padding\-top: 1px\;\"\>\n\<a href = \"http:\/\/www\.unicef\.org\"\>\n\<img style = \"margin\-bottom: 20px\; border: none\;\" src = \"http:\/\/www\.polisource\.com\/images\/Banner_BackgroundNotes\.gif\"\>\n\<\/a\>\n\<\/p\>\n\n\<br\>/s;

print "-------------------$test--------------------\n\n\n";The problem might be that my command exceeded the allowable length. Could someone remind me what the limit is?

P.S. Yes, I was in the correct directory when it didn't work.

Terra
07-23-2005, 08:36 AM
Could someone remind me what the limit is?
The argument limit, which includes the environment, is 128KB...

If it exceeded that limit, then you would have received the following error message:
"Argument list too long"

Most likely you have a quoting issue in your perl statement... Long lines like that can be tricky to get right...

Personally, I'd use 'find' to find those files and write them to a list... Then add a loop to the perl script to read the contents of that list and act on each file... Another thing to be careful of the way you have it written, is any filenames with spaces in them won't be handled properly... You should get in the habit of using:
find ....... -print0 | xargs -0 .......

--
Terra
--Zero is my hero--
FutureQuest

Wassercrats
07-23-2005, 09:55 AM
I tried escaping more things at the command line and tried it on just one file (no spaces in the name) and it still didn't work. I'm not a command line person, so I think I'll use a cgi script for this.

Incidently, I tried a search and replace yesterday without the g modifier and once it worked and the other time it replaced two things, but maybe I did something wrong.

Wassercrats
07-24-2005, 03:28 PM
I wrote a script that does what I want, but I'm still investigating why the command line script doesn't work. The first script below doesn't change the test file, but the second one does. They both use the same regex, and I expected them to both change the file. The file they should be changing is at http://www.polisource.com/documents/BackgroundNotes/TEST/1831pf.shtml . Feel free to copy the file and the code to your website for testing purposes.#!/usr/bin/perl5.6.1
##########################################

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

system ('cd /big/dom/xpolisource/www/documents/BackgroundNotes/TEST;find * -name \'1831pf.shtml\' | xargs perl -pi -e "s/\<body\>.*\<\!\-\-\#include virtual\=\"\/cgi\-bin\/Banner\_BackgroundNotes\.pl\" \-\-\>\<br\>/\<body style \= \"margin\-top\: 1px\; padding\-top\: 1px\;\"\>\n\n\<p style \= \"text\-align\: center\; margin\-top\: 1px\; padding\-top\: 1px\;\"\>\n\<a href \= \"http\:\/\/www\.unicef\.org\"\>\n\<img style \= \"margin\-bottom\: 20px\; border\: none\;\" src \= \"http\:\/\/www\.polisource\.com\/images\/Banner\_BackgroundNotes\.gif\"\>\n\<\/a\>\n\<\/p\>\n\n\<br\>/s;"');#!/usr/bin/perl5.6.1
##########################################################

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

$Direc = "/big/dom/xpolisource/www/documents/BackgroundNotes/TEST/1831pf.shtml";

open(IN, $Direc);
@file = <IN>;
close IN;

$Webpage = join ("", @file);

$Webpage =~ s/\<body\>.*\<\!\-\-\#include virtual\=\"\/cgi\-bin\/Banner_BackgroundNotes\.pl\" \-\-\>\<br\>/\<body style = \"margin\-top: 1px\; padding\-top: 1px\;\"\>\n\n\<p style = \"text\-align: center\; margin\-top: 1px\; padding\-top: 1px\;\"\>\n\<a href = \"http:\/\/www\.unicef\.org\"\>\n\<img style = \"margin\-bottom: 20px\; border: none\;\" src = \"http:\/\/www\.polisource\.com\/images\/Banner_BackgroundNotes\.gif\"\>\n\<\/a\>\n\<\/p\>\n\n\<br\>/s;

open(LOG, ">$Direc");
print LOG "$Webpage";
close(LOG);The first script worked when I shortened the regex to just replace the word "head" with "xxxxxxxx" but if the long regex works in the second script, I don't see why it won't work in the first.

I don't know if it's safe or not to publish the full Unix path to my files, but I took a chance. I've done it before. It's actually a relative full path, so maybe it's not full.