-1

I want to read a file and inside the file have number words. I have to do operation math. but I dont know why my script doesn't work.

Exemple : seven one minus six one

the answer in the terminal :

$ 10

sorry for my script I start to learn how to script ..

#!/bin/sh

a=( ["zero"]="0" ["one"]="1" ["two"]="2" ["three"]="3" ["four"]="4"
    ["five"]="5" ["six"]="6" ["seven"]="7" ["eigth"]="8" ["nine"]="9" ["plus"]="+" ["minus"]="-" ["time"]="*" ["divide"]="/" ["modulo"]="%" )

if [ $# -eq 0 ]; then
    echo " Error : missing a file"
    exit 1
else
    if [ ! -f "$1" ]; then
            printf 'Error:file'" "$1" ""dont exist\n'" >&2
            exit 2
    fi
fi


while read line 
  do
    for word in $line
      do
            case $ in
                       +)
                            echo $(( $a + $a ))
*)
                            echo $(( $a *$a ))

$ cat file

nine six one plus one two

$ 973

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.