The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calling Perl from C pkusumam High Level Programming 3 04-01-2009 05:16 AM
[PERL] Running unix commands within Perl Scripts userix Shell Programming and Scripting 1 05-28-2008 07:06 PM
rsh commands not getting executed from Solaris 10 System to AIX System jumadhiya SUN Solaris 25 01-22-2007 05:26 AM
Unix commands in perl script athri UNIX for Dummies Questions & Answers 1 07-14-2006 10:31 AM
URL calling in PERL DeepakXavier Shell Programming and Scripting 1 01-04-2006 11:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-05-2006
new2ss new2ss is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 133
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 (permalink)  
Old 04-05-2006
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 (permalink)  
Old 04-05-2006
new2ss new2ss is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 133
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 (permalink)  
Old 04-05-2006
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
Actually it does :-)
system returns a success/failure value which 'if' tests
  #5 (permalink)  
Old 04-05-2006
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0