![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
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";
}
|
|
||||
|
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";
|
|
||||
|
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"; } |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|