Results 1 to 8 of 8
I'm trying to call a system command in perl and am having an issue with it.
Here's an example of a command i'd like to call:
Code:
sed -i '4 ...
- 05-20-2010 #1
[SOLVED] Calling a system command in Perl
I'm trying to call a system command in perl and am having an issue with it.
Here's an example of a command i'd like to call:
this is the section in my perl script where I create the variable and call it:Code:sed -i '4 c\192.168.1.4 www.something.com' hosts
The $line, $ip, and $host variables are working fine becasue I can replace that section with "prints" and they come out fine. I imagine the problem is where I am creating the $doit variable.Code:$doit = `sed -i '$line c\$ip $host hosts'` system($doit);
Thanks!
- 05-20-2010 #2
this may sound silly, but have you tried
Code:system("$doit");linux user # 503963
- 05-20-2010 #3
That gave me the same extremely vague error:
Line 73 is the following:Code:syntax error at ./sedtest line 73, near "system" Execution of ./sedtest aborted due to compilation errors.
Code:system($doit);
- 05-20-2010 #4
well in what you posted
there is a semicolon missing at the endCode:$doit = `sed -i '$line c\$ip $host hosts'`
i wouldn't use the ticks either. i'd make it more like:
does that work any better?Code:system("sed -i '$line c\$ip $host hosts'");linux user # 503963
- 05-20-2010 #5
It's getting closer. I got rid of the command variable and changed the system() part to have the full command like you listed it. It errored so I cahnged the system() part to just print and it appears that the "\" before "c" is disappearing in the print.
- 05-20-2010 #6
yeah that '\' has special meaning as an escape character in the shell.
perhaps parts of it need to be fed in as a scalar variable.
you can also try exec() instead of system().Code:$sedline = "'$line c\$ip $host hosts'"; system("sed -i $sedline");
basically we just have to trick it into not seeing that '\' as an escape character. you may also try using '\\'. Sometimes I run into the same problem when processing '?' in URLs that are passed to system().linux user # 503963
- 05-20-2010 #7
dur, i didnt even think to use the second backslash. That did it (plus moving the single quotation mark from the end of hosts back to $host).
- 05-20-2010 #8
So Just in case this extremely obscure question is needing to be answered by anybody else, here's the final solution:
Code:system("sed -i '$line c\\$ip $host' hosts");


