bash question
Bob Bell
bobbell at zk3.dec.com
Mon Sep 23 12:00:42 EDT 2002
On Mon, Sep 23, 2002 at 11:44:05AM -0400, pll at lanminds.com <pll at lanminds.com> wrote:
> In a message dated: Mon, 23 Sep 2002 11:37:40 EDT
> "Thomas M. Albright" said:
>
> >If the due date is greater than today, it outputs the message "Due in x
> >days." If the due date is less than today I get a message saying "You
> >are -x days past due." How can I convert the negative number into a
> >positive one in bash?
>
> Well, assuming that 'x' is a variable which contains some whole
> number representing the number of days in which 'it' is due, a
> negative number representing an over-due 'it', then:
>
> abs_x=`echo '$x' | sed s/-//`
>
> ought to do it.
Alernatively, to stay within bash:
neg_x=$(( -1 * x ))
or
neg_x=$(( -x ))
or
abs_x=$(( x < 0 ? -x : x ))
or
abs_x=${x#-}
--
Bob Bell <bobbell at zk3.dec.com>
-------------------------------------------------------------------------
"It's easy to solve the halting problem with a shotgun. :-)"
-- Larry Wall, creator of the Perl programming language
More information about the gnhlug-discuss
mailing list