The UNIX and Linux Forums  


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
sed: removing backticks from certain lines bjb UNIX for Dummies Questions & Answers 5 02-27-2008 11:20 AM
Backticks within backticks? jeriryan87 Shell Programming and Scripting 3 07-02-2007 05:38 PM
Need Help w/PERL system command lorik Shell Programming and Scripting 1 12-06-2006 08:09 PM
perl - system command reggiej Shell Programming and Scripting 5 09-26-2005 08:52 PM
Perl run system command gdboling Shell Programming and Scripting 1 09-02-2003 11:30 PM

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 07-28-2005
gjkeenan
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Perl - backticks v system in if statements

Can someone explain the difference between backticks and system when
evaluated in these if statements:


sub getDate {
print "start date\n";
if ( system("/bin/date") ) {
print "can't get date\n";
exit(2);
}
print "finish date\n";
}


Returns the following:

start date
Thu Jul 28 12:13:59 EST 2005
finish date




While this:

sub getDate {
print "start date\n";
if ( `/bin/date` ) {
print "can't get date\n";
exit(2);
}
print "finish date\n";
}


Returns this:

start date
can't get date



Also - I'm not sure I understand the logic of the top if statement. If system() runs OK shouldn't the next two lines of the if statement then run i.e. the print and the exit?
  #2 (permalink)  
Old 07-28-2005
Unbeliever Unbeliever is offline
Registered User
  
 

Join Date: Jul 2005
Location: England
Posts: 183
In the first example: system('/bin/date');

prints the date to STDOUT and returns the exit status of /bin/date, in this case '0'. So the following if condition fails. You have already printed out the date simply by calling system('/bin/date').

In the second example `/bin/date' returns the string that you get by running the command but doesnt print anything to STDOUT. Since any non null string (except for the string that contains just '0') evaluates to true in perl your if clause prints 'Cant get date'.

At a rough guess its the second way you want to use but like this:


Code:
sub getDate
{
  my $date;
  print "start date\n";
  $date=`/bin/date`;
  if ( !$date )
  {
    print "can't get date\n";
    exit(2);
  }
  print $date;
  print "finish date\n";
}

  #3 (permalink)  
Old 07-28-2005
gjkeenan
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Thanks for explaining that. Makes sense now.

The date thing was just an example. The actual code is from this tape changer script and I couldn't understand the logic if the chio command ran OK why the print and exit didn't run after it.


Code:
if ( system("/bin/chio -f $changerDevice move slot ".($tape-1)." drive 0
") ) {
    print "$progname: cannot '/bin/chio -f $changerDevice move' tape
 $tape into drive 0\n";
    exit(2);
}
print LOG &do_time(), ": leave: Load\n";

  #4 (permalink)  
Old 07-29-2005
tjlst15 tjlst15 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 17
Just a note, when using system in perl, it returns the exit code multiplied by 256. So, if a command returns 1, system("command") will return 256. So, to get the real return value, divide by 256.
  #5 (permalink)  
Old 07-31-2005
ErNci ErNci is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 24
Yes, as tjlist said, the percecitonal way really should be

if ( ( system("/bin/chio -f $changerDevice move slot ".($tape-1)." drive 0
") ) / 256 ) {
print "$progname: cannot '/bin/chio -f $changerDevice move' tape
$tape into drive 0\n";
exit(2);
}
print LOG &do_time(), ": leave: Load\n";

And in the first example, the 2nd example would be

sub getDate {
print "start date\n";
my $buff = '';
if ( ($buff = `/bin/date`) eq ''){
print "can't get date\n";
exit(2);
}
print "finish date\n";
}
  #6 (permalink)  
Old 07-31-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
The OP is only checking if the return value from the function or call is not 0. So really the division by 256 is superfluous. Unless he gets return 0 (in which case the division will also be 0), anything else is an error.
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 09:17 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