Calculation


Table of Contents

cmd:expr 
Comments 
Usage 
Help 
test run 
cmd:expr string operation 
Usage 
Help 
Trying History 
expr how to match 
expr how to match 
matching standalone string against regexp 
Command-Line math 
Command-Line math 
Print Ascii lists 
Ascii list in man page 
Ascii table 
Ascii table 
cmd:Hexdump and formatting it's output 
Canonical Usage 
Using format file 
Hex & Decimal 
Hex to character 
Hex to Decimal in "standard" Unix - How ? 
Hex to Decimal in "standard" Unix - How ? 
Shell Calculator 
cmd:dc, Desk Calculator 
Usage 
Help 
cmd:bc 
hex to dec 
Help 
DOS Debug like viewer under Unix 
cmd:od 

cmd:expr 

Comments 

Need separate parameters 

$ expr "7 > 5"
7 > 5
$ expr 7 \> 5
1

Can't work on real numbers 

$ expr 2 + 3
5
$ expr 2 + 3.1
expr: non-numeric argument

Usage 

As testing 

$ [ `expr 7 \> 5 ` ] || echo aaa
$ [ `expr 7 \> 5 ` ] && echo aaa
aaa
$ if [ `expr 7 \> 5 ` -eq 1 ]; then echo aaa; else echo bbb; fi
aaa
$ if [ `expr 7 \< 5 ` -eq 1 ]; then echo aaa; else echo bbb; fi
bbb
$ if [ `expr 7 \> 5 ` ]; then echo aaa; else echo bbb; fi
aaa
$ if [ `expr 7 \< 5 ` ]; then echo aaa; else echo bbb; fi
aaa

Arithmetic 

$ expr \( 5 + 10 \) / 2
7
$ vv=1
$ vv=`expr $vv + 1`
$ svs vv
vv=2
$ expr `echo 2` '>' 1; echo $?
1
0
$ expr `echo 1` '>' 1; echo $?
0
1
$ expr `echo '   2'` '>' 1; echo $?
1
0

— space is not a problem

$ a='aaa'
$ expr "$a" = aaa
1
$ expr "$a" = aab
0
$ b=5
$ expr "$b" + 5 \>= 10
1
$ expr "$b" + 5 \>= 11
0
$ p="version.100"
$ expr "$p" : '.*'
11
$ [ `expr "$p" : '.*'` -gt 0 ] && echo yes
yes
$ expr "$p" : '\(.*\)'
version.100
$ expr "$p" : '[a-z]*'
7
$expr "$p" : '\([a-z]*\)'
version
# return no more than 4 char...
$ x='12345'
$expr "$x" : '\(....\)' \| "$x"
1234
$  x='123'
$ expr "$x" : '\(....\)' \| "$x"
123
$ expr "$x" : '\(....\)'
$ expr "-b4" : '-b\([0-9]*\)' \| 2
4
but '+' nok:
$ expr "-b4" : '-b\([0-9]+\)' \| 2
2
$
$ a=/opt/SUNWconn/chorus_4.0LA/chorus-powerpc/os/root
$ expr $a : '.*/\(.*\)' '|' $a
root
$ v="version1234."
$ expr $v : '.*\([0-9]+\).*'
$ expr $v : '.*\([0-9][0-9]*\).*'
4
$ expr $v : '.*n\([0-9]+\).*'
$ expr $v : '.*\(n[0-9]+\).*'
$ expr $v : '.*\(n[0-9][0-9]*\).*'
n1234
$ expr $v : '.*n\([0-9][0-9]*\).*'
1234

Help 

:
      Perform pattern matching. Its arguments are coerced to
      strings and the second one is considered to be a regular
      expression, with a `^' implicitly added at the beginning. The
      first argument is then matched against this regular
      expression. If the match succeeds and part of the string is
      enclosed in `\(' and `\)', that part is the value of the :
      expression; otherwise an integer whose value is the number
      of characters matched is returned. If the match fails, the :
      operator returns the null string if `\(' and `\)' are used,
      otherwise 0.
Only one `\(' and `\)' pair can be used.

The following may be used to print the non-directory part of the file name stored in variable a ( if the value does not contain `/', use $a):

expr $a : '.*/\(.*\)' '|' $a

Note the quoted shell metacharacters.

expr returns the following exit status:

0 if the expression is neither null nor 0,
1 if the expression is null or 0,
2 for invalid expressions.

test run 

1<<, >>

$ tty
/dev/pts/0
$ expr `tty` : '/dev/pts/'; echo $?
9
0
$ expr `tty` : '/dev/tty'; echo $?
0
1

2<<, >>

$ TTY=`tty`
$ if `expr $TTY : '/dev/tty' > /dev/null` ; then echo from tty; else echo not form tty; fi
not form tty
$ TTY='/dev/tty1'
$ if `expr $TTY : '/dev/tty' > /dev/null` ; then echo from tty; else echo not form tty; fi
from tty

documented on: Sun 03-28-99 09:47:32