### Thread: absolute value fuction in unix

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2005
Posts
16
Rep Power
0

#### absolute value fuction in unix

hi guys,
is there a function that helps in calculating the absolute value of a number????
2. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Jul 2004
Location
Middle Europa
Posts
1,200
Rep Power
14
Originally Posted by smokingguns
hi guys,
is there a function that helps in calculating the absolute value of a number????
sure
i think, you cannot use abs()
so on cmd-line, try
echo "your-number/1" |bc
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2004
Location
Prague, Czech Rep.
Posts
117
Rep Power
13

Originally Posted by guggach
sure
i think, you cannot use abs()
so on cmd-line, try
echo "your-number/1" |bc
I have tried 2/1 | bc

and got, of course

-2/1 | bc
ksh: -2/1: not found

which is not exactly what was required. If something is not ready made in the system, write it yourself, for instance:

:
# Function to evaluate the absolute value of a number
abs () {
# Check for numeric input
if expr \$1 + 0 2>/dev/null 1>&2 ; then
# Is the number negative?
if [ \$1 -lt 0 ] ; then
echo `expr 0 - \$1`
else
echo \$1
fi
return 0 # OK
else
return 1 # Not a number
fi
}

A=-10; abs \$A || echo \$A not a number
A=121; abs \$A || echo \$A not a number
A=1x1; abs \$A || echo \$A not a number

This script was tested under Linux with bash, but it must work under ksh and sh, too.

Have a fun ZL
4. No Profile Picture
......@.........
Devshed Beginner (1000 - 1499 posts)

Join Date
Jun 2004
Posts
1,345
Rep Power
56
try this as well:
Code:
echo "-.233" | awk ' { if(\$1>=0) { print \$1} else {print \$1*-1 }}'
5. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Jul 2004
Location
Middle Europa
Posts
1,200
Rep Power
14
Originally Posted by zlutovsky
I have tried 2/1 | bc

and got, of course

-2/1 | bc
ksh: -2/1: not found

which is not exactly what was required. If something is not ready made in the system, write it yourself, for instance:

:
# Function to evaluate the absolute value of a number
abs () {
# Check for numeric input
if expr \$1 + 0 2>/dev/null 1>&2 ; then
# Is the number negative?
if [ \$1 -lt 0 ] ; then
echo `expr 0 - \$1`
else
echo \$1
fi
return 0 # OK
else
return 1 # Not a number
fi
}

A=-10; abs \$A || echo \$A not a number
A=121; abs \$A || echo \$A not a number
A=1x1; abs \$A || echo \$A not a number

This script was tested under Linux with bash, but it must work under ksh and sh, too.

Have a fun ZL
i said
echo "333.7777/1" | bc
if you have a normal shell under a normal unix-os, you get
333

NOTA: BC and his brother DC, in unix, are known since 1978
Last edited by guggach; July 15th, 2005 at 04:44 PM.
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2004
Location
Prague, Czech Rep.
Posts
117
Rep Power
13
Originally Posted by guggach
i said
echo "333.7777/1" | bc
if you have a normal shell under a normal unix-os, you get
333

NOTA: BC and his brother DC, in unix, are known since 1978

But guggach, have you ever heard what the absolute value is?
You are presentig something what was not required, namely the integer part of a number. First read the question and then shoot.

Regards
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2004
Location
Prague, Czech Rep.
Posts
117
Rep Power
13
Originally Posted by jim mcnamara
try this as well:
Code:
echo "-.233" | awk ' { if(\$1>=0) { print \$1} else {print \$1*-1 }}'

Yes, awk is universal, but the question was to write a function. It is no problem for an experienced shell programmer to write some envelope to your statement to provide the required function.

Regards
8. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Jul 2004
Location
Middle Europa
Posts
1,200
Rep Power
14
Originally Posted by zlutovsky
But guggach, have you ever heard what the absolute value is?
You are presentig something what was not required, namely the integer part of a number. First read the question and then shoot.

Regards
ok, i did not check negative values

my way remain BC

val=-123.098

echo "a=\$val/1;if(0>a)a*=-1;a"|bc
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2009
Posts
1
Rep Power
0

#### what about this one

\$VALUE=\${VALUE/-/}

note: i am working under bash
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2007
Posts
168
Rep Power
8
Originally Posted by MischaMolhoek
\$VALUE=\${VALUE/-/}
That does not work for me

v=-123.45
echo \${v/-/}
ksh: \${v/-/}: bad substitution

However this works
echo \${v#-}
123.45