LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-14-2012, 11:02 AM   #1
jlien
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Rep: Reputation: Disabled
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...
 
Old 02-14-2012, 11:08 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,960

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by jlien View Post
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.
 
Old 02-14-2012, 11:47 AM   #3
jlien
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
TB0ne,

I did include #!/bin/bash, my entire shell script is very similar to what you entered, the : was a mistake...sorry.
 
Old 02-14-2012, 12:39 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,960

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by jlien View Post
TB0ne,

I did include #!/bin/bash, my entire shell script is very similar to what you entered, the : was a mistake...sorry.
Ok...so is it working now? If not, what error(s) are you getting, and if you post the code, we can probably help.
 
Old 02-14-2012, 01:45 PM   #5
jlien
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
I don't get any errors, the file just isn't created.

This is the shell script:::

#!/bin/bash
df -h > df.tmp
<><><><><><><><><><><><><><><><><><><><><><><><><>

This is the perl code calling the shell script:::

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);
}
 
Old 02-14-2012, 01:59 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
So ? there is no /home/user/df.tmp ?
 
1 members found this post helpful.
Old 02-14-2012, 02:07 PM   #7
jlien
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
correct
 
Old 02-14-2012, 02:29 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,960

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by jlien View Post
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
 
1 members found this post helpful.
Old 02-15-2012, 09:33 AM   #9
jlien
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
TB0ne,

I tried to put it in /tmp, but it isn't there.

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
 
Old 02-15-2012, 11:17 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,960

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by jlien View Post
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
Perl is fairly easy to pick up...check out http://www.perlmonks.com. An awesome resource for perl programming. There is also a perl module, Filesys::diskspace which can help you (http://search.cpan.org/~ftassin/File...s/DiskSpace.pm).

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.
 
Old 02-16-2012, 01:06 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
I'd also point out that these 2 lines are redundant
Code:
system("/home/user/df.sh");

open(my $df_handle, "df -h|") or die "cant run df:$!";
Either

1. run an external prog to a file and parse the file
OR
2. call df direct and parse output

See these links to learn Perl
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
http://www.tizag.com/perlT/index.php

You also have the options of http://perldoc.perl.org/IPC/Open2.html or http://perldoc.perl.org/IPC/Open3.html
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
run shell script from perl kzcom Linux - Networking 4 09-04-2010 08:30 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 10:56 AM
how to run a shell script in the perl script sharad Linux - General 1 05-24-2006 04:23 AM
a script run through a shell program... okeyla Linux - Newbie 1 09-15-2005 11:06 AM
run shell script in c program u4u Linux - General 1 02-14-2004 03:48 PM


All times are GMT -5. The time now is 04:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration