Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix commands in perl scripts 1

Status
Not open for further replies.

Meatsim

Programmer
Oct 9, 2001
13
US
Hello folks, I have a question:

I have a perl script that takes a few lines of text and splices them into an executable Unix command. It does so by writing these lines into a file called change.tmp which is created at runtime.

However, I can't seem to execute change.tmp using

system "change.tmp";

in the perl script. I can execute change.tmp from the command line, and it works there, but I'd like to get it working in the script so that I can delete change.tmp after the script finishes.

Any help would be appriciated.

Thanks,
Meatsim
 
What are the symptoms - what happens when you run the script that does the system "change.tmp"; command?

A couple of things:
1) read the perldocs on system by doing

$ perldoc -f system

2) you should be error checking your system command to see if it fails - something like

system "command" || die "system failed trying to execute command: $?";

3) you may need to specify the absolute path to the command you want to execute, like

system "/path/to/command" || die "system failed trying to execute command: $?";

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks for your help! I added an error check to the command, but it still wouldn't execute. However, the script wouldn't die either, so I think that perl thinks it is executing the command with no success.

I also tried to give the full directory to the command, but I had little luck there. Is there an easy way to get your current directory inside Perl? I tried
Code:
$curdir = system "pwd";
system "$curdir\/$changefile";
but that didn't work.

Thanks,
Meatsim
 
What platform(OS) are you on? Are you sure the absolute path you are specifying is correct - I know on Windows it can be a pain to get path names correct since it can take a while to figure out how to properly specify the delimiter between directories.

Try putting some "print"s in to help figure out what is hanging the program.
Hardy Merrill
Mission Critical Linux, Inc.
 
Right now I'm on a Sun Solaris 3.6 box. As it turned out, printing out the path revealed that $curdir in the code snippet above is actually '0' instead of the path. As well, the script doesn't actually hang, it runs from start to end, but doesn't execute that darn file.

Needless to say, this has been a bit of a head-scratcher.

Thanks,
Meatsim
 
Instead of using "system", try using backticks, like:

$curdir = `pwd`;

Backticks returns a list of lines that are the output of the command, so normally you would receive the output into an array, but since pwd returns one line, receiving the output into a scalar should be(?) fine.

You could also try using system like this:

system("./$changefile") || die "Can't execute $changefile: !?";

I'm not 100% sure that will work, but by prepending $changefile with "./", you are saying execute $changefile in the current directory. But it's always better(and more predictable) to specify the entire absolute path to the file you want to execute.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
It worked! I tried using

system("./$changefile") || die "Can't execute $changefile: !?";

and the script promptly executed the file at runtime!

Sigh. Laid low by a slash and a dot. Thank you very much for your help!

Thanks,
Meatsim
 
errmm

I thought that system() worked the other way 'round to everything else.

perl functions return non-zero for success and zero for failure

except that - system emulates the unix shell and returns zero for success and passes any error code from the command it runs as it's return code

so I've always written:

system("./$changefile") && die "Can't execute $changefile";

am I wrong? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Actually, to get the real return code you have to divide the result by 256 (or you can use >> since it's a multiple of two). I don't like the "system && die" form very much because it doesn't TELL you what the reason (result code) is, so I usually do it like this:
Code:
$rc = system("some command");
$rc /= 256;
die("some command returned $rc") if $rc;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
yeah, I like that Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top