Perl calling unix system commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl calling unix system commands
# 1  
Old 04-05-2006
Perl calling unix system commands

Code:
if ( system ("/bin/cat $File1 >> $File2") ) {
print("#WARNING RAISED : /bin/cat File1 >> File2  - FAILURE!\n"); }

I came across this code, would appreciate if someone can tell me if my understanding is correct?

the perl code tell the system to cat file 1 into file 2, if command fails, print the warning statement?
I am a little confused by the test condition:
if ( system ("/bin/cat $File1 >> $File2") ) Is it testing for successful writing or unsuccessful writing ?
# 2  
Old 04-05-2006
Quote:
Originally Posted by new2ss
[code]
I am a little confused by the test condition:
if ( system ("/bin/cat $File1 >> $File2") ) Is it testing for successful writing or unsuccessful writing ?
I think the code is correct. You actually tested it on your machine. Did you get the expected behaviour of having the error printed on error?

You do not have to be confused by the semantics of this usage. It appears bizarre, but it actually works that way because system() returns a value that depends on the return value of the command executed. If you read the perldoc for system() you will find that the actual return status is encoded in the least significant 8 bits, and higher bits will be set in the event of other errors. In general, most Unix shell commands have the convention of 0 return status being successful, while other values reflect error status. Therefore, the construct you quoted actually means to detect anything non-zero, that indicates error status. Of course it deviates from typical programming practice that zero is considered false, error, or anything like NULL.
# 3  
Old 04-05-2006
Yup, i tested it.

So the if ( system ("/bin/cat $File1 >> $File2") ) Is testing for unsuccessful writing.

it sure does not follow the normal if else format.
# 4  
Old 04-05-2006
Actually it does :-)
system returns a success/failure value which 'if' tests
# 5  
Old 04-05-2006
Quote:
Originally Posted by new2ss
it sure does not follow the normal if else format.
As I said, it's just because of the peculiarity of system() and the shell return status. If you replace the command executed with one that returns a non-zero value to indicate success status for whatever reasons, then you will need to adjust the test to reflect that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable into perl system commands

Hi guys, I'm having issues getting the following snippet of my script to work and was hoping for some suggestions. I'm trying to pass a variable in perl system with wget. This is what I need help with: #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime) ; my... (3 Replies)
Discussion started by: timj123
3 Replies

2. Shell Programming and Scripting

Execution problem unix commands in Perl CGI

I am trying to run SSH , mkdir and other unix commands using Perl CGI. But i am not able to Execute these commands. Please help me out !!!! SSH and mkdir is necessity for me. I will be thankful to you...!!!!! I am trying like: In perl CGI file i am writing like: @list = `ssh... (28 Replies)
Discussion started by: Navrattan Bansa
28 Replies

3. UNIX for Dummies Questions & Answers

Calling commands with ksh

Hi, I am not able to run below command on linux, it however works on solaris. If anyone knows the reason and a solution for it can you please let me know ? Linux ----- $> ksh 'echo hi' ksh: echo hi: No such file or directory $> which ksh /usr/bin/ksh Solaris ------ $> ksh 'echo... (2 Replies)
Discussion started by: krishnaux
2 Replies

4. Shell Programming and Scripting

calling perl commands in shell

i have four commands 1. perl -MCPAN -e shell 2. o conf prerequisites_policy follow 3. o conf commit 4. exit I am attempting to streamline a bunch of yum commands and cpan installations and want to remove the confirmation portion of the cpan these four commands will do just that. my... (2 Replies)
Discussion started by: murphybr
2 Replies

5. UNIX for Advanced & Expert Users

Oracle (11gr2) calling unix commands (aix)

I have an Oracle database running on AIX, and I have a procedure that is calling OS commands from an oracle (and it's not working anymore)... so, there was an Java stored proc in Oracle CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED COMMON."Host" as import java.io.*; public class Host {... (1 Reply)
Discussion started by: bongo
1 Replies

6. Shell Programming and Scripting

Running unix commands in a perl script

Executing two unix commads via perl script one after another e.g: make clean bsub -i -q short make have tried using exec but the second command doesnt executes (1 Reply)
Discussion started by: rajroshan
1 Replies

7. Programming

calling UNIX commands C/C++

hi Guys, I am planning to write a program which can be able to execute scripts/commands which needs a user to be root. Note: we are not interested to use sudoers option. .e.g. The requirement can be explaind as: A normal UNIX system user cannot execute above command(veritas cluster... (3 Replies)
Discussion started by: Asteroid
3 Replies

8. Shell Programming and Scripting

Running unix commands through perl

Hi all, In the directory '/temp/chris' the following files exist: chris.tar, chris.txt What i am trying to do is to assign the 'chris.tar' filename in an argument through perl, in order to do that i use the system command: $file=system("ls /temp/chris/*.tmp), but in the '$file' the exit... (2 Replies)
Discussion started by: chriss_58
2 Replies

9. Shell Programming and Scripting

[PERL] Running unix commands within Perl Scripts

I understand that in order to run basic unix commands I would normally type at the prompt, I would have to use the following format system(ls -l); or exec(ls -l); But when I actually try to use the command, the script fails to compile and keeps telling me there is an error with this line. ... (1 Reply)
Discussion started by: userix
1 Replies

10. UNIX for Dummies Questions & Answers

Unix commands in perl script

I am totally new to unix commands but I need to understand the following command which is a part of a perl script..what does this mean? myPwd = $(pwd) myTracker = $myPwd/myTracker.out exec > $myTracker 2>&1 (1 Reply)
Discussion started by: athri
1 Replies
Login or Register to Ask a Question