using Systme and exec to run shell script in perl program
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
using Systme and exec to run shell script in perl program
I have been trying to run a shell script from a perl program for practice. My shell script is simple, :df -h > df.tmp" I have tried using both system and exec and have not had any luck. I want to create a tmp file so I can display it later. Thanks...
I have been trying to run a shell script from a perl program for practice. My shell script is simple, :df -h > df.tmp" I have tried using both system and exec and have not had any luck. I want to create a tmp file so I can display it later. Thanks...
Without seeing the perl code and the full shell script code, there's little we can tell you.
The syntax for a perl system call is:
Code:
system("<name of shell script/program goes here>");
A properly written shell script would be:
Code:
#!/bin/bash
df -h > /some/path/df.tmp
Without the /bin/bash, and without specifying the path to the tmp file, you'll likely encounter problems. The colon before the df is also puzzling.
I don't get any errors, the file just isn't created.
This is the shell script:::
Code:
#!/bin/bash
df -h > df.tmp
Ok. Now the question is: are you running the perl script from the same directory where you've got the shell script?? Because as above, the "df -h" command will create the df.tmp file in whatever directory you're in, providing you have permissions to create a file. If not, it'll run the command, but not return any output. A better thing to do would be to run "df -h > /home/user/df.tmp", to be SURE to create the file in the same place every time, and be sure it's a read/write location. Also, you could create it in /tmp, which is exactly what that directory is for, and would be better if you want other users to be able to run the same script.
Quote:
This is the perl code calling the shell script:::
Code:
sub Get_sys_disk_space{
use strict;
print "<B>System disk space</B><HR>";
system("/home/user/df.sh");
open(my $df_handle, "df -h|") or die "cant run df:$!";
while (<$df_handle>) {
# if (/^\s*(\/\w+)\s+[\0-9]/) {
if (/^\s*(\/.*)\s+(\d+)/ ) {
my ($filesys, $size, $used, $avail, $usep, $mntd) = ($1, $2, $3, $4, $5, $6);
{
print "<TABLE border='1' cellpadding='1' cellspacing='5'>";
print "<TR bgcolor='#c0c0c0'>";
print "<TD>File System</TD>";
print "<TD>Size</TD>";
print "<TD>Used</TD>";
print "<TD>Avail</TD>";
print "<TD>Use %</TD>";
print "<TD>Mnt on</TD>";
print "</TR>\n" ;
print "<TR>";
print "<TD>$1</TD>";
print "<TD>$2</TD>";
print "<TD>$3</TD>";
print "<TD>$4</TD>";
print "<TD>$5</TD>";
print "<TD>$6</TD>";
print "</TR>\n";
}
print "</TABLE>";
}
}
return(1);
}
You could also set the $df_handle variable to BE the system call:
Code:
my $df_handle = `df -h`;
Note, those are backticks, not single-quotes. And you've got other options, too...
Code:
$output = `program args`; # collect output of a system command into one multiline string
@output = `program args`; # collect output of a system command into an array, one line per element
TB0ne,
I tried to put it in /tmp, but it isn't there.
Then something is wrong in general. Can you execute that shell script from a terminal window and get results? Also, try putting the absolute path to to the df command in there...probably /bin/df, to rule out something odd in a path.
Quote:
As you can see, I tried to use $df_handle, but I had alot of trouble breaking the data down to something that was readable when I displayed it. As you can tell I am new to perl and have alot to learn. Get to have Java thrown at me in another month as well.
3
Also, it looks like you're trying to display the data in a table via HTML...but you're not actually opening an output file for writing, or including any of the other HTML stuff you'd need to build a page.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.