[Za-pm] Using perl to call a bash script

Nico Coetzee nico at itfirms.co.za
Mon May 12 15:11:05 CDT 2003


To add a root cronjob, log in as root and then do:

# crontab -e

You can then add:

*/5 * * * * /bin/grep pppd /var/log/syslog > /var/www/html/file ; /bin/chmod 
644 /var/www/html/file ; /bin/chown apache.apache /var/www/html/file 

NOTE: The above is all in one line. 

You can then include the /var/www/html/file as usual in PHP. The above cron 
will execute at exactly 5 minute intervals.

Regarding the Perl stuff:

There are two ways: 

First:

$ perl -e '$res=`echo "This is a test shell command"`; print "$res";'

This is the easiest way, and is also known as the back-tick method. 
Interesting to note is that the return result stored in $res will include the 
newline ( \n ) character. To get rid of it - use: chomp( $res );

If you want to capture multiple lines, and process one line at a time:

@lines = `/bin/df -B 1024`;
shift( @lines ); # get rid of the header line
foreach $line ( @lines ) {
   chomp( $line ); # remove newlines
   ( $dev, $size, $used, $avail, $usep, $mountpoint ) = split( /\s+/, $line );
   if ( int( $avail ) > 1000000 ) { 
      print "Still plenty of space left on $dev ( $mountpoint )\n";
}

The above commands will store each line returned as a row in the array @lines. 
We then get rid of the first line ( header line ) with the shift command, and 
start to loop through each row - one at a time. With each loop, we first get 
rid of the newline, and then we split the row up in elements ( space 
seperated ) with the split command. Each element is stored in a var, and we 
can use that var as in the example where we printed a info message.

You can use this same technique to "pretty-fy" your output on your web page, 
if you use Perl instead of PHP, although I belief PHP has similar 
capabilities.

The scond, more secure way, of calling commands is with the system command. 
See 'perldoc -f system' for the complete documentation.

ADSL? I wish that was available in my area!

Cheers

On Monday 12 May 2003 21:19, Steve Cox -dig wrote:
> Two fold message,
>
> Howdie all, just testing is this works,
>
> Secondly, I use some php, but I am interested, can you call a bash script
> from perl (probably). If so, as a user/or from a webpage can I also call
> raw bash comands like if the perl script is in the cgi-bin how could I
> include a raw bash command (as root if possible) to pull the following to a
> file:
>
> steve at steve]# grep pppd /var/log/syslog > anewfile
>
> I cant get a root cronjob working and from a user I cant pull records from
> the /var/log/syslog
>
> A simple example of typical output at www.dig.co.za go to "my home pc" then
> "statistics" this at the moment is run manualy every so often. My script
> works when I run it in bash manually, just as root its a no go.
>
> Anyways, nice to have a perl list, and I know for fact it will help me
> plenty. I dig php but prefer perl for some stuff, so an integration of both
> is cool.
>
> For what its worth, I run a small lan at home off a ADSl conection, 4
> Mandrake 9.1 machines, one is a gateway samba server (P1 100mhz 32meg ram)
> and one other P4 1.7 dual boots off seperate hdd to win98 for the time
> being. Others plain Mandrake.
>
> Oh b4 we have any flamewars - the best distro:
> LINUX !
>
> Regards
> Steve Cox
> _______________________________________________
> Za-pm mailing list
> Za-pm at mail.pm.org
> http://mail.pm.org/mailman/listinfo/za-pm

-- 
Nico Coetzee

http://www.itfirms.co.za/
http://za.pm.org/
http://forums.databasejournal.com/

To the systems programmer, users and applications serve only to provide a
test load.




More information about the Za-pm mailing list