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 Fri 25 Jul 2014, 09:06
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 Posts_count  
Author Message
scsijon

Joined: 23 May 2007
Posts: 1020
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

Edited_time_total
Back to top
View user's profile Send_private_message Visit_website 
amigo

Joined: 02 Apr 2007
Posts: 2217

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: 895
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: 1020
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_website 
GustavoYz


Joined: 07 Jul 2010
Posts: 895
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:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


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