bash as calculator

not so much, but:

not so bad

arithmetic

arithmetic expansion provides a powerful tool for performing integer arithmetic operations in scripts

translating a string into a numerical expression is relatively straightforward using backticks, double parentheses, or let:

#!/bin/bash

echo `expr 8 + 3`
echo `expr 8 / 3`
echo `expr 8 - 3`
echo `expr 8 \* 3`
echo `expr 8 % 3`

x=`expr 5 + 3`
echo $x
let x=x+2
echo $x

((b = 23))
echo $b
((b++))
echo $b
((--b))
echo $b

exit 0

select

tool for building menus

select x in read_file write_file del_file add_file quit
  do 
    echo $x 
    break 
  done
this prompts the user to enter one of the choices presented in the variable list

note that select uses the $PS3 prompt (#? ) by default, but this may be changed

if in [list] is omitted, then select uses the list of command line arguments ($@) passed to the script or to the function

integer comparisons

-eq
equal to
if [ $a -eq $b ]

-ne
not equal to
if [ $a -ne $b ]

-gt
greater than
if [ $a -gt $b ]

-ge
greater than or equal to
if [ $a -ge $b ]

-lt
less than
if [ $a -lt $b ]

-le
less than or equal to
if [ $a -le $b ]

<
less than (within double parentheses)
(($a < $b))

<=
less than or equal to (within double parentheses)
(($a <= $b))

>
greater than (within double parentheses)
(($a > $b))

>=
greater than or equal to (within double parentheses)
(($a >= $b))

=
equal to
if [ $a = $b ]

!=
not equal to
if [ $a != $b ]

<
less than, in ASCII alphabetical order
if [[ $a < $b ]]
if [ $a \< $b ]
note that the "<" needs to be escaped within a [ ] construct.

>
greater than, in ASCII alphabetical order
if [[ $a > $b ]]
if [ $a \> $b ]
note that the ">" needs to be escaped within a [ ] construct

#!/bin/bash

echo `expr 3 = 3`
echo `expr 3 != 3`
echo `expr 10 \> 3`
echo `expr 10 \<= 3`

x=10
y=20

if [[ $x < $y ]]
then echo "x less than y"
else echo "y less than x"
fi

exit 0

compound comparison

-a
logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true

-o
logical or
exp1 -o exp2 returns true if either exp1 or exp2 is true

these are similar to the Bash comparison operators && and ||, used within double brackets
[[ condition1 && condition2 ]]

loops

#!/bin/bash

for x #in $1 $2 
do 
  echo `expr $x + 2`;
  echo `expr $x - 2`;
done

for a in `seq 10`
  do
    echo -n "$a "
  done  

for a in {1..10}
  do
    echo -n "$a "
  done  

read var0
limit=20
while [ "$var0" -lt "$limit" ]  
    do
      echo -n "$var0 "
      var0=`expr $var0 + 1`   # var0=$(($var0+1))      also works
                              # var0=$((var0 + 1))     also works
                              # let "var0 += 1"        also works
                              # Various other methods  also work
      echo
    done                      
}

end_cond=10
until [ "$var1" = "$end_cond" ]
do
  echo "Input variable # "
  echo "($end_cond to exit)"
  read var
  echo "variable # = $var"
  echo
done  

exit 0

the break command may optionally take a parameter. a plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop

the continue command, similar to break, optionally takes a parameter. a plain continue cuts short the current iteration within its loop and begins the next. a continue N terminates all remaining iterations at its loop level and continues with the next iteration at the loop, N levels above

branches


#!/bin/bash

select x in one two three 
do
  case $x in
    one  ) echo $1; break;; 
    two  ) read y; echo `expr $1 + $y`;;
    three) exit 0;;
  esac
done

functions

#!/bin/bash

func1 ()  {
  if [ $1 -gt $2 ]
    then echo `expr $1 - $2`;
    else echo `expr $2 + $1`;
  fi
}

func2 () {
  case $1 in
      1) echo `expr $2 + $3`;;
      2) echo `expr $2 + $3`;;
      *) echo "mistake";;
  esac
}

func1 5 7
func2 1 4 6

exit 0

the function must be defined before usage