Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 24 Apr 2014, 20:46
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
[solved] perl - how do I run an external command?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [5 Posts]  
Author Message
scsijon

Joined: 23 May 2007
Posts: 997
Location: the australian mallee

PostPosted: 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

Last edited by scsijon on Tue 19 Feb 2013, 23:07; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 2173

PostPosted: 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
View user's profile Send private message 
GustavoYz


Joined: 07 Jul 2010
Posts: 886
Location: .ar

PostPosted: 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
View user's profile Send private message 
scsijon

Joined: 23 May 2007
Posts: 997
Location: the australian mallee

PostPosted: 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
View user's profile Send private message Visit poster's website 
GustavoYz


Joined: 07 Jul 2010
Posts: 886
Location: .ar

PostPosted: 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
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [5 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0470s ][ Queries: 12 (0.0051s) ][ GZIP on ]