CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Running shell commands within perl script

 



gjarms
New User

Dec 20, 2013, 4:01 AM

Post #1 of 8 (2173 views)
Running shell commands within perl script Can't Post

Hi Perl Gurus,

This is my first post, so please forgive me if i violate any norms.

I have been trying to run the shell command like the one below

$cmd=[[`echo hello`]] || [[`echo hellohello`]] && sleep 10;

actually i need to check the return value if null or not, if not null the the succeeding command should run.

The above command will run fine in bash shell, when i tried within perl like `$cmd` it issues error [[ not found.

But when i use eval command it seems to be working.

eval $cmd, does not produce any error but "sleep" command is not called.

Can someone help to solve this issue?

Regards,
gjarms


FishMonger
Veteran / Moderator

Dec 20, 2013, 6:38 AM

Post #2 of 8 (2167 views)
Re: [gjarms] Running shell commands within perl script [In reply to] Can't Post

Your command does not work for me in a bash shell.


Quote
[root@099-91-RKB-2 ~]# [[`echo hello`]] || [[`echo hellohello`]] && sleep 10
bash: [[hello]]: command not found
bash: [[hellohello]]: command not found


I'm not sure what shell command(s) you want to run.

Do you need to capture the output of the shell command or the exit code of the command?

If you need the exit code, then use the system function to run your shell command.
http://perldoc.perl.org/functions/system.html

If you need to capture the output of the command, then use the ` ` backticks operator or the qx() operator.
http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators


BillKSmith
Veteran

Dec 20, 2013, 6:41 AM

Post #3 of 8 (2165 views)
Re: [gjarms] Running shell commands within perl script [In reply to] Can't Post

Please post a complete Perl program so we can duplicate your pboblem. Always use

Code
use strict; 
use warnings;


I am nearly certain that your problem is with improper use of quotes, but I cannot be sure without a full program. Note: The pragmas may help you solve this yourself.
Good Luck,
Bill


gjarms
New User

Dec 22, 2013, 10:59 AM

Post #4 of 8 (2052 views)
Re: [FishMonger] Running shell commands within perl script [In reply to] Can't Post

Hi,

Please assign the command to a variable like this: cmd=[[`echo hello`]] || [[`echo hello-hello`]] && echo hi
hi

it works and print "hi".

I am trying to achieve the same by first reading the command

perl -e '$cmdd="[[`echo hello`]] || [[`echo hello-hello`]] && echo hi"; qx($cmdd)'
sh: [[hello]]: command not found
sh: [[hello-hello]]: command not found

Please quide me how can i modify this so that same funtionality can be achieved.

Please note that i dont need "exit status/code" but return value thats the reason i am using square brackets [[ ]].

Regards,
gjarms


FishMonger
Veteran / Moderator

Dec 22, 2013, 4:28 PM

Post #5 of 8 (2024 views)
Re: [gjarms] Running shell commands within perl script [In reply to] Can't Post

I won't have access to a *nix system until Tue, so I can't test that portion until I get back to work.

Why are you wanting/trying to assign shell script vars in a perl call? I almost never work with shell coding, but can you assign a var at the command line like that and if so, why do you need/want to do that in the Perl script?

What are you really needing to accomplish i.e., what is the actual command that you need to execute?

I'm willing to bet that whatever you're doing in the shell code can be done directly in perl without having to "shell out".


(This post was edited by FishMonger on Dec 22, 2013, 4:29 PM)


gjarms
New User

Dec 22, 2013, 9:01 PM

Post #6 of 8 (2006 views)
Re: [FishMonger] Running shell commands within perl script [In reply to] Can't Post

Hi,

We have a 1000+ lines of perl code to achieve a functionality which is basically to parse number of .xml files which will have number of projects entries. This script will check the repository and if there are updates on the upstream projects it will trigger a replication to many hosts. the original entry in the perl script is this.


Code
$cmd = "git --git-dir=$localrepo fetch $remoterepourl $refspec && git --git-dir=$localrepo fetch --tags $remoterepourl $refsp 
ec && ssh hostname replication start $project_to_replicate";

system($cmd)


The above snippet will run all the commands separated by &&, but the last command ssh hostname replication start $project_to_replicate should not run if there are no updates from the first 2 commands.

Regards,
gjarms


(This post was edited by gjarms on Dec 22, 2013, 9:04 PM)


FishMonger
Veteran / Moderator

Dec 23, 2013, 9:33 AM

Post #7 of 8 (1941 views)
Re: [gjarms] Running shell commands within perl script [In reply to] Can't Post

The problem as stated is not Perl related, it's a shell related problem.

The first step in solving this issue is to look at it without using Perl.

How would you solve this issue if you were to run the command directly at the system prompt instead of the system call in the script? The answer to that question would be the answer used in the system call in the script.

I don't know enough about git or the shell to answer that question, but my guess is that you'll need to split off the ssh call and test the outcome of the Git calls.

If you want a more Perlish solution, then you'll need/want to use one of the Git related modules and possibly one of the SSH modules.

http://search.cpan.org/~msouth/Git-0.02/lib/Git.pm
http://search.cpan.org/search?query=git&mode=all

http://search.cpan.org/~rkitover/Net-SSH2-0.53/lib/Net/SSH2.pm


FishMonger
Veteran / Moderator

Dec 24, 2013, 7:40 AM

Post #8 of 8 (1871 views)
Re: [gjarms] Running shell commands within perl script [In reply to] Can't Post

If the suggestions in my prior post don't work out for you, then my next suggestion would be to execute each of those git commands separately via backticks or the qx() operator saving their output and then parse that output to confirm that there are updates. Then do or not do the ssh call based off the results of that parsing.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives