LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2009, 07:03 AM   #1
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Rep: Reputation: 15
Running shell script through perl


Hi,

Can any one tell me how to run a shell script through perl with example?
 
Old 03-17-2009, 07:20 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by tucs_123 View Post
Can any one tell me how to run a shell script through perl with example?
It's a bad idea, in my experience. It's better either to run the shell script as a shell script or to rewrite it in Perl. (That means more than simply passing commands to the shell via Perl. It means writing it to Perl's strengths and weaknesses.)

Can you show the script and say a bit more about why you want to do it this way?

Last edited by Telemachos; 03-17-2009 at 07:21 AM.
 
Old 03-17-2009, 07:34 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Telemachos View Post
It's a bad idea, in my experience. It's better either to run the shell script as a shell script or to rewrite it in Perl. (That means more than simply passing commands to the shell via Perl. It means writing it to Perl's strengths and weaknesses.)

Can you show the script and say a bit more about why you want to do it this way?
Maybe the OP just meant running any executable through 'system' ?
 
Old 03-17-2009, 07:45 AM   #4
Frullet
LQ Newbie
 
Registered: Feb 2009
Location: Sydney,Australia
Distribution: Slackware
Posts: 12

Rep: Reputation: 0
system ("./bashscript");

But as before, it is best to rewrite the shell script in perl, or just stick to the shell script itself.
 
Old 03-17-2009, 07:55 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Frullet View Post
system ("./bashscript");

But as before, it is best to rewrite the shell script in perl, or just stick to the shell script itself.
If the shell script works, why rewrite ?

If something new is to be written, why write it in shell ?
 
Old 03-17-2009, 08:12 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
If the shell script works, why rewrite ?
not pertaining to this thread. Few reasons (not just shell scripts) i can think of.
1) the script works, but take up too much resources. therefore needs to rewrite to make it better.
2) company is revamping their scripting standards. scripts will be written in one particular language for a "uniform" environment. They don't want shell, perl, ruby etc scripts all lying around.
3) the script works, but due to new business processes and requirement, it has to be rewritten to cater for these new processes.
 
Old 03-17-2009, 08:20 AM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by Sergei Steshenko View Post
Maybe the OP just meant running any executable through 'system' ?
You may be right, but the OP asked how to do this for a 'shell script', and by that I took him to mean a script for Bash or Ksh or whatever.

One thing to add to Ghostdog's reasons: a well-written Perl program should be more portable than a shell script (which will often rely on particular implementations of sed, grep and company).

Last edited by Telemachos; 03-17-2009 at 08:21 AM.
 
Old 03-17-2009, 08:36 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Telemachos View Post
...
One thing to add to Ghostdog's reasons: a well-written Perl program should be more portable than a shell script (which will often rely on particular implementations of sed, grep and company).
I agree with that - whenever I try to write something in shell in an hour I'm asking myself "why haven't I written this in Perl ?".

I do, however, in certain cases generate shell scripts from my Perl scripts, but these shell scripts are just plain sequential commands.

The point is that contents of these scripts are not known until some pretty complex processing is done in Perl; after that the shell scripts are kind of "exported intelligence" from Perl, and can be used separately.
 
Old 03-19-2009, 07:43 PM   #9
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
Try this:

execlp("/bin/bash", "/bin/bash","[Your script]","-C", (char*)NULL);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 09:56 AM
running a shell script which invokes another shell program!!! raghu123 Programming 1 08-04-2008 09:10 AM
how to run a shell script in the perl script sharad Linux - General 1 05-24-2006 03:23 AM
Shell Script, C, C++, or Perl ? mrpc_cambodia Programming 5 08-12-2004 10:18 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration