2

I am new at bash scripting, I need to write a script capable of taking a file with a given text (written numbers and arithmetic operators) and transform these words into integers and ex: +, -, *, %.

The example that I have is a file with the following text : one two three plus nine eight seven. The script needs to change these words and transform them to 123 + 987, then printing the result 1110.

So far I have this, but I am not sure how to compare each word in the file to the ones in my code with the correct values to be able to do the operations.

#!/bin/bash

fileCal="$1"  #file in_calc is entered

d=( ["zero"]="0" ["one"]="1" ["two"]="2" ["three"]"3" ["four"]="4"
["five"]"5" ["six"]="6" ["seven"]="7" ["eight"]="8" ["nine"]="9" ["plus"]="+" ["minus"]="-" 
["times"]="\*" ["div"]="/" ["modulo"]="%") 

if [ "$#" == 0 ]; then
    echo $error
elif [ -f $fileCal ]; then
    read -a $fileCal 
fi

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.