Perl - backticks v system in if statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - backticks v system in if statements
# 1  
Old 07-28-2005
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?
gjkeenan
# 2  
Old 07-28-2005
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  
Old 07-28-2005
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";

gjkeenan
# 4  
Old 07-29-2005
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  
Old 07-31-2005
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  
Old 07-31-2005
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl combine multiple map statements

I have a file like file. file.TODAY.THISYEAR file.TODAY.LASTYEARI want to substitute the words in caps with their actual values so that output should look like file.140805 file.140805.2014 file.140805.2013For this I am reading the file line bye line in an array and using multiple map... (1 Reply)
Discussion started by: sam05121988
1 Replies

2. Shell Programming and Scripting

Nesting backticks

I'm trying to make a dialog window that prints the output of grep that takes the output of find. Unfortunately my nested backticks don't work. Here is the dialog window: dialog --stdout --title "test" --backtitle "test" --msgbox "Test:\n `grep -l "${tablica}" `find $string``" 16 60I think I... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

3. Shell Programming and Scripting

SSH and Backticks [solved]

I have been testing a new script and cannot figure out why my `cat spath` will not execute on the remote machine? sudo ssh -p 22344 -o "PasswordAuthentication no" -o "HostbasedAuthentication yes" -l testuser 192.168.1.6 "find `cat spath` -depth" cat: spath: No such file or directory but... (0 Replies)
Discussion started by: metallica1973
0 Replies

4. Shell Programming and Scripting

[solved] using backticks to call bash from perl

Hi all, Here is my code: my $x = `bash -c \" ls -l filename | awk '{print \$5}'\"`; print "$x\n"; This will run the first part of the bash script but not the awk command. It therefore gives output of: -rw-r--r-- 1 root root 13619200 2012-04-25 08:16 filename I am actually trying to... (0 Replies)
Discussion started by: free2rhyme2k
0 Replies

5. Red Hat

perl backticks: can't redirect output.

Hi everyone. This is a bit of a perl/linux mixed question. I am trying to redirect STDOUT of chsh by using the following line of perl code. system ("chsh -s /sbin/nologin $testing 1>/dev/null"); This should redirect STDOUT to /dev/null but it won't do that for some odd reason. Any ideas or... (6 Replies)
Discussion started by: austinharris43
6 Replies

6. Programming

String if statements - Perl

So far, all I have been able to come up with is: if ($check=~/no/ || $check=~/n/) but the problem with this, is that it looks for any character and the if statement is true. So if I wanted to check for an argument "-help" or lets say a variable string that could be a file name. Then... (2 Replies)
Discussion started by: adelsin
2 Replies

7. Shell Programming and Scripting

perl if elsif statements

I have having problems with an IF statement in my perl script: if ($model eq "N\\A") {} elsif ($kernel =~ m/xen/) { $model = ("Virtual Machine\n")}; What i am trying to accomplish is if the model is set to "N\A" and the kernel variable has xen somewhere in it i would like to change... (3 Replies)
Discussion started by: insania
3 Replies

8. Shell Programming and Scripting

use statements and system glitch

hi, i have a perl script that runs as a cron job... Once in a while, the perl script fails with: Can't locate <module>.pm in @INC (@INC contains: .............) because one of the perl modules specified in the "use" statements is unavailable due to an NFS glitch. Is there some... (1 Reply)
Discussion started by: Andrewkl
1 Replies

9. Shell Programming and Scripting

Am I abusing backticks?

I'm always concerned I might be abusing backticks within my scripts. A current script I'm writing has this for example: stripscriptname=`echo $scriptname | sed 's/\(.*\)\..*/\1/'` stripsearch=`echo $searchpattern | tr -d ' ,/'` Both of these variables are set inside the script (in fact,... (2 Replies)
Discussion started by: mglenney
2 Replies

10. Shell Programming and Scripting

Backticks within backticks?

Hi, I'm trying to do something like this: range= `expr `date '+%m'` - 1` and it does not work. How can I tell it to evaluate an expression within another expression evaluation? I was at first worried that `date '+%m'` would return a string but apparently expr does the math okay normally, so the... (3 Replies)
Discussion started by: jeriryan87
3 Replies
Login or Register to Ask a Question