Find the answer to your Linux question:
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 ...
  1. #1
    Linux User cheesecake42's Avatar
    Join Date
    Jan 2007
    Location
    Panama City, FL
    Posts
    364

    [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:
    Code:
    sed -i '4 c\192.168.1.4  www.something.com' hosts
    this is the section in my perl script where I create the variable and call it:
    Code:
    $doit = `sed -i '$line c\$ip    $host hosts'`
    system($doit);
    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.

    Thanks!

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    this may sound silly, but have you tried

    Code:
    system("$doit");
    linux user # 503963

  3. #3
    Linux User cheesecake42's Avatar
    Join Date
    Jan 2007
    Location
    Panama City, FL
    Posts
    364
    That gave me the same extremely vague error:

    Code:
    syntax error at ./sedtest line 73, near "system"
    Execution of ./sedtest aborted due to compilation errors.
    Line 73 is the following:
    Code:
    system($doit);

  4. #4
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    well in what you posted
    Code:
    $doit = `sed -i '$line c\$ip    $host hosts'`
    there is a semicolon missing at the end

    i wouldn't use the ticks either. i'd make it more like:

    Code:
    system("sed -i '$line c\$ip     $host hosts'");
    does that work any better?
    linux user # 503963

  5. #5
    Linux User cheesecake42's Avatar
    Join Date
    Jan 2007
    Location
    Panama City, FL
    Posts
    364
    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.

  6. #6
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    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.

    Code:
    $sedline = "'$line c\$ip     $host hosts'";
    system("sed -i $sedline");
    you can also try exec() instead of system().

    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

  7. #7
    Linux User cheesecake42's Avatar
    Join Date
    Jan 2007
    Location
    Panama City, FL
    Posts
    364
    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).

  8. #8
    Linux User cheesecake42's Avatar
    Join Date
    Jan 2007
    Location
    Panama City, FL
    Posts
    364
    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");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...