Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to execute a linux system command with perl?

by introdev (Initiate)
on Nov 20, 2018 at 12:00 UTC ( #1226063=perlquestion: print w/replies, xml ) Need Help??

introdev has asked for the wisdom of the Perl Monks concerning the following question:

Hi there does not work: my @cmd = ('/sbin/route'); push @cmd, 'add'; push @cmd, 'default'; push @cmd, 'gw', push @cmd, '172.16.0.200', push @cmd, 'dev'; push @cmd, 'enxb827eb95400d'; system(@cmd); why? thanks & best regards, jan
  • Comment on How to execute a linux system command with perl?

Replies are listed 'Best First'.
Re: How to execute a linux system command with perl?
by marto (Cardinal) on Nov 20, 2018 at 12:10 UTC

    A cleaned up copy of what you posted:

    #!/usr/bin/perl use strict; use warnings; my @cmd = ('/sbin/route'); push @cmd, 'add'; push @cmd, 'default'; push @cmd, 'gw', push @cmd, '172.16.0.200', push @cmd, 'dev'; push @cmd, 'enxb827eb95400d'; system(@cmd);

    some lines end with commas when they should be semicolons:

    #!/usr/bin/perl use strict; use warnings; my @cmd = ('/sbin/route'); push @cmd, 'add'; push @cmd, 'default'; push @cmd, 'gw'; push @cmd, '172.16.0.200'; push @cmd, 'dev'; push @cmd, 'enxb827eb95400d'; system(@cmd);

    If in doubt, print what your variables contain. Tutorials->PerlMonks for the Absolute Beginner/Basic debugging checklist.

      Also:

      my @cmd = qw[/sbin/route add default gw 172.16.0.200 dev enxb827eb9540 +0d];

      or

      my @cmd = split/\ +/, "/sbin/route add default gw 172.16.0.200 dev enx +b827eb95400d";

      perl -e 'use MIME::Base64; print decode_base64("4pmsIE5ldmVyIGdvbm5hIGdpdmUgeW91IHVwCiAgTmV2ZXIgZ29ubmEgbGV0IHlvdSBkb3duLi4uIOKZqwo=");'
Re: How to execute a linux system command with perl?
by haukex (Bishop) on Nov 20, 2018 at 12:08 UTC

    You haven't shown any error messages, so we can't say what's going wrong. (Also, please use <code> tags to format code.) It looks like you're not checking the return value of system for errors, you should do that with the code shown in its documentation, or with a module such as IPC::System::Simple. I wrote about the topic of running external commands at length, with sample code, here.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1226063]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2021-04-20 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?