Visit Our UNIX and Linux User Community


Perl script 'system' linking to local shell script not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script 'system' linking to local shell script not working
# 1  
Old 10-16-2009
Perl script 'system' linking to local shell script not working

Trying to figure out why this works:

printpwd.pl
Code:
#!/usr/bin/perl
use CGI::Carp qw( fatalsToBrowser );
print "Content-type: text/html\n\n";


$A = system("pwd");
$A = `pwd`;
print "$A\n";

^^actually that works/breaks if that makes any sense.. i get the working directory twice but when i comment out the second variable declaration $A = `pwd`;.. it prints out the success 0. Smilie But at least it printed something out from the cl. Now this confuses me...


hellow.pl
Code:
#!/usr/bin/perl
use CGI::Carp qw( fatalsToBrowser );

print "Content-type: text/html\n\n";

system("hellow");
 
open(HTMLFILE, "hellow.html") || die "
  <center><H3>Sorry, I cannot open HTML pollfile.</H3></center>";
while (read(HTMLFILE, $buffer, 16384))  #Print the Poll HTML file
{
    print $buffer;
}
close(HTMLFILE);

hellow.sh
Code:
#!/bin/sh

echo "</head>
<body text="#FF0000" bgcolor="#000000">
<b>TEST</b>" > hellow.html


The second pair of scripts, when i run hellow locally it works fine generating the html fine but when i remove it and try to have it called from the perl script, perl doesn't err on line 6, it complains of line 8??? grrrr Smilie
# 2  
Old 10-16-2009
Perl doesn't auto-complain if something non-critical doesn't work. If you want to call a shell script, but it can't find it (say, because you forgot to type the '.sh' that's part of the filename), system will fail with an error code, which you as the programmer have to catch and act upon (example in perldoc -f system).

And the ouput of the file can be written simpler as
Code:
open (HTMLFILE, "hellow.html");
while ($buffer = <HTMLFILE>)
{
    print $buffer;
}
close HTMLFILE;

or even
Code:
open (HTMLFILE, "hellow.html");
print while HTMLFILE;
close HTMLFILE;

# 3  
Old 10-16-2009
yea i know what you mean about perl and it not complaining but i guess that's the reason for the rant. perl doesn't care about my 'critical' step in the code. but i'm not lazy, i'll figure it out some way.

I tried to simplify the output as you suggested in both examples but for some reason my browser just spins when I try to load the page even with the system call commented out. I had to kill this on my box.

Code:
52977 www           1 111    0  2796K  2172K CPU0   0   0:21 92.61% perl5.8.7

Looking at the man, i tried to follow but got lost at `$?`

Quote:
You can check all the failure possibilities by inspecting $?
like this:

if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}
So my next task is to learn how to take this tidbit to test this and any other future shell scripts against perl. My command 'hellow' still works fine when typed directly into the cl. I forgot to mention that the hellow.sh was copied, moved, and exported into my shell env ~/home/root/bin.. that's why i'm somewhat dumbfounded... pwd works but hellow doesn't still and i'm clueless. I'm hoping that testing it in perl with the code above might give me some more info. Need some input on what to put where. Should i just replace all `?` to `hellow`??
# 4  
Old 10-16-2009
$? is a variable that holds the return code of the last system command, pipes, ..., pretty much the same as in the shell. By inspecting that you can check if your command succeded or not.

As for your 'hellow' script, your user might have the path to it in it's environment. But the user the webserver is running as (www) most probably doesn't. So it's always a good idea to use the absolute path, and ensure that the user 'www' has read & execute rights on the file. If you still have problems, add the 'holy trinity' to your script, and run it on the command line.
Code:
# The 'holy trinity'
use strict;
use warnings;
use diagnostics;

As for Perl spinning out of control: it might have to do with the unchecked open. I left out the success check in my examples for brevity, but you should always check the return code of it.
# 5  
Old 10-16-2009
hehe thanks pludi, newbie error Smilie www didn't have the proper permissions.. but let me ask this.. the holy trinity i found quite useful. I fixed the variable declaration it complained about with 'buffer' and it pointed out the permissions error. Without the permissions detail I would've been lost.

Now I know that the symbol declaration issue wasn't the source of my problems but is it generally good practice to check/clean up perl scripts using it?? or do ppl generally ignore some entries they know are common and are not an issue since when you ask it to be strict it's only doing what you told it?

Last edited by pludi; 10-16-2009 at 05:43 AM.. Reason: language
# 6  
Old 10-16-2009
As a rule, I use two of them in every script (strict and warnings), 'diagnostics' when developing / debugging but I drop it for production code. Sure, it might be more comfortable to code without them, but sooner or later you'll miss a typo that has a nasty side effect. Some warnings may be ignored (such as "uninitialized value in concatenation"), except that they might clutter your output/log and distract from any serious message.


I've got a couple other tips:
  • Use perl -c occasionally. It won't run your script, but check the syntax. Good to catch any missing semi-colons.
  • Use perltidy on your code to make it easier to read. Does a syntax check, too.
  • Occasionally, run perlcritic for some best practices.

Previous Thread | Next Thread
Test Your Knowledge in Computers #159
Difficulty: Easy
The Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP) need two ports per host for full-duplex, bidirectional traffic.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Except script to run a local shell script on remote server using root access

local script: cat > first.sh cd /tmp echo $PWD echo `whoami` cd /tmp/123 tar -cvf 789.tar 456 sleep 10 except script: cat > first #!/usr/bin/expect set ip 10.5.15.20 set user "xyz123" set password "123456" set script first.sh spawn sh -c "ssh $user@$ip bash < $script" (1 Reply)
Discussion started by: Aditya Avanth
1 Replies

2. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

3. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

4. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

5. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

6. Shell Programming and Scripting

executing perl script from another perl script : NOT WORKING

Hi Folks, I have 2 perl scripts and I need to execute 2nd perl script from the 1st perl script in WINDOWS. In the 1st perl script that I had, I am calling the 2nd script main.pl =========== print "This is my main script\n"; `perl C:\\Users\\sripathg\\Desktop\\scripts\\hi.pl`; ... (3 Replies)
Discussion started by: giridhar276
3 Replies

7. UNIX for Dummies Questions & Answers

Perl script not working

Hi Experts!! I have written a very simple script in perl.The script is : $ cat 1.pl #!/usr/bin/perl print "Hi there!\n"; When i ran the above perl script it is showing the following error: $ perl 1.pl -ksh: cd: bad substitution Can anybody,help on this ....as why this script is... (1 Reply)
Discussion started by: Amey Joshi
1 Replies

8. Shell Programming and Scripting

Executing shell script on local machine

Hi guys, I need to run and test some shell script. At work, i work on ksh. I don't have any such software/client installed at home and i cannot always connect to work from home. At home i have Windows Vista. Is there a free and reliable software where i can run my ksh script? Please let me... (4 Replies)
Discussion started by: jakSun8
4 Replies

9. Shell Programming and Scripting

Shell script to create local homes

Hi I am trying to write a login script for network based clients (OSX) that looks up local accounts eg admin, root, etc and exits the script so that it doesn't apply to them. Then for everyone else I make folders eg movies, music, etc that are placed in local harddrive rather than the default... (11 Replies)
Discussion started by: Steve Adcock
11 Replies

10. UNIX for Dummies Questions & Answers

shell script, reading and resetting local variables

Hello, I have a problem with trying to run a shell script that reads in user input, validates, and sets to a 'default' value if the input is not valid. I cannot get the portion of resetting to a default value to work. These lines are skipped, and the value of x is still whatever the user... (1 Reply)
Discussion started by: b888c
1 Replies

Featured Tech Videos