943,844 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 3325
  • Perl RSS
Apr 5th, 2007
0

Multiple scripts in one file

Expand Post »
Is it possible to have a perl script and a bash script in one file? Like:

Perl Syntax (Toggle Plain Text)
  1. #!/bin/perl
  2. print "This is perl";
  3. #exit perl
  4.  
  5. #!/bin/bash
  6. echo "This is bash"
  7. exit 0

This is probably just a complicated solution to a simple problem. I have a perl script, and at the end of it, I want to make a couple calls that I normal write in the shell.

I tried this but it does not work:

Perl Syntax (Toggle Plain Text)
  1. $command = "ps";
  2. system $command;

Any help is appreciated greatly.
- Dano
Similar Threads
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Apr 5th, 2007
0

Re: Multiple scripts in one file

I found a solution to my own problem now. =P

This works:

Perl Syntax (Toggle Plain Text)
  1. #!/bin/perl
  2. use Shell;
  3. $command = echo("parameters", "go", "here");
  4. print $command; #performs echo
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Apr 5th, 2007
0

Re: Multiple scripts in one file

I'm not sure if this is a wise way to do this, but it should work:


Perl Syntax (Toggle Plain Text)
  1. $ENV{PATH}='/bin/perl:/bin/bash';
  2. open (PS, 'ps |') or die "$!";
  3. print <PS>;
  4. close PS;
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Apr 5th, 2007
0

Re: Multiple scripts in one file

this does not work because the system() function does not return the output of the process:

Perl Syntax (Toggle Plain Text)
  1. $command = "ps";
  2. system $command;

You use backtiks `` or qx// to capture process output:

Perl Syntax (Toggle Plain Text)
  1. $output = qx/ps/;
  2. print $output;


But on further review of interprocess communications, perldocs recommends using this safe construct:

Perl Syntax (Toggle Plain Text)
  1. open PS, "-|", "ps" or die $!;
  2. print <PS>;
  3. close PS;

or stick with the Shell module
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: String replacement in a file
Next Thread in Perl Forum Timeline: Tomcat-Perl-Perl





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC