| Author |
Message |
scsijon
Joined: 23 May 2007 Posts: 948 Location: the australian mallee
|
Posted: Mon 18 Feb 2013, 23:47 Post_subject:
[solved] perl - how do I run an external command? |
|
I need to run something like this:
| Code: |
$package_category = (./support/find_cat $package_name $package_description)
|
format I am trying to match is:
| Code: | | output_variable = relative_file_path/filename_to_run $input_variable1 $input_variable2 |
it works ok via the console, but not in my opensuserpm2ppm script
it just errorrs out with
syntax error at ./opensuserpm2ppm line 149, near "(." and line149 is the above one of course.
I've tried using () {} and "" but it doesn't seem to work.
I've chased on the w5c training docs, but can't see anything I can use
any ideas please?
thanks
Edited_time_total
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1776
|
Posted: Tue 19 Feb 2013, 13:23 Post_subject:
|
|
Try here:
http://www.asicguru.com/scripting/perl-tutorial/running-external-programs/50/
and here:
http://www.perlhowto.com/executing_external_commands
|
|
Back to top
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 868 Location: .ar
|
Posted: Tue 19 Feb 2013, 15:16 Post_subject:
|
|
It depends if you need/want some results back.
Most cases, this is the best:
| Code: | | my $stuff = `cat /this/file`; |
Perl uses 'sh' by default, so avoid bashisms.
Also, if writing something complex to escape, something that requires back-ticks, for example, use 'qx/`shell code here`/.
The system function gives only the return status of the shell code executed.
_________________

|
|
Back to top
|
|
 |
scsijon
Joined: 23 May 2007 Posts: 948 Location: the australian mallee
|
Posted: Tue 19 Feb 2013, 23:06 Post_subject:
|
|
thanks all and especially GustavoYz
eventual code I used is
| Code: | | $package_category = `./support/find_cat $package_name $package_description` ; |
with the ` from the key with ~ and not the " key.
|
|
Back to top
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 868 Location: .ar
|
Posted: Wed 20 Feb 2013, 14:15 Post_subject:
|
|
Exactly...
You can also save the whole command and its arguments into a single variable, and then backtick it. Something like this is perhaps more practical for re-use:
| Code: | if ($opts{f}){
$comm .= ' ' . "$opts{f}";
} else {
$comm .= ' ' . $default_file;
}
my $result = `$comm`;
|
using Getopt::Std (core module), so then you can specify different files using a '-f' switch, for example:
| Code: | | ./script.pl -f /path/to/file.sh |
_________________

|
|
Back to top
|
|
 |
|