[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Request for enhancement: links to 'true' and 'false'

From: Bob Proulx
Subject: Re: Request for enhancement: links to 'true' and 'false'
Date: Sat, 20 Apr 2013 11:28:04 -0600
User-agent: Mutt/1.5.21 (2010-09-15)

Jeffrey Streifling wrote:
> This additional feature would be used with external scripting
> interpreters that use 0 and 1 as truth values, such as GNU bc.  The
> useful idiom would be
> if `bc <<< "$VAL1 > $VAL2""`

Of course the use of `...` backticks makes even a traditionalist guy
like me cringe.  I much prefer to see $(...) used over `...`.  (I do
use bc all of the time as a quick desk calculator.)

If you are comparing values the bc program isn't the traditional way.
It would be more traditional to use 'expr'.

  if expr 1 '>' 2; then

A simple way to check and print the return:

  $ expr 1 '>' 2 && echo yes || echo no

  $ expr 1 '<' 2 && echo yes || echo no

But even expr is now obsoleted by built in shell math.  Use ((...)) to
perform the math in the shell with no external program calls.  (And no
need to redirect the stdout output of expr to hide it either.)  And
best of all shell metacharacters do not need to be quoted since it is
a shell builtin.

  $ ((1 < 2)) && echo yes || echo no

  $ ((1 > 2)) && echo yes || echo no

  if ((1 < 2)); then

Your example case would be:

  if (($VAL1 > $VAL2)); then

Isn't that nicer than using the original?

  if `bc <<< "$VAL1 > $VAL2""`

Inside ((...)) variables are automatically expanded.  I like the
consistency of names always having a '$' in front so I personally
always include it.  It makes searching for uses easier for me.  But
you can leave out the dollar signs in ((...)) and the variable will be
expanded.  I find the inconsistency grating though.

  if ((VAL1 > VAL2)); then


reply via email to

[Prev in Thread] Current Thread [Next in Thread]