954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multiple scripts in one file

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

#!/bin/perl
print "This is perl";
#exit perl

#!/bin/bash
echo "This is bash"
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:

$command = "ps";
system $command;


Any help is appreciated greatly.
- Dano

nanodano
Junior Poster in Training
78 posts since Feb 2005
Reputation Points: 36
Solved Threads: 2
 

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

This works:

#!/bin/perl
use Shell;
$command = echo("parameters", "go", "here");
print $command; #performs echo
nanodano
Junior Poster in Training
78 posts since Feb 2005
Reputation Points: 36
Solved Threads: 2
 

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

$ENV{PATH}='/bin/perl:/bin/bash';
open (PS, 'ps |') or die "$!";
print <PS>;
close PS;
KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

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

$command = "ps";
system $command;


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

$output = qx/ps/;
print $output;

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

open PS, "-|", "ps" or die $!;
print <PS>;
close PS;


or stick with the Shell module

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You