I have somewhat wierd requirement to be resolved in Unix script.
supose i have a file containing
a=4
b=3
c=a+b
d=c+a
e=a+b+5+d
I have to get output like
a=4
b=3
c=7
d=11
e=23
I have tried using AWK using the logic that whatever in RHS variable avaiable I will replace it with ${variable}
for example
e=a+b+5+d
will comeout as
e=${a}+${b}+5+${d}
But I am not sure how to get value of "a,b,d" in the currently running shell in AWk. Used “source” keyword after writing the a=4 thing to file. but it is not working.
Please suggest some other way because I don’t think my way is going to work.
If you have the formulas in a file
expr.sh, then this will work:Explanation:
e=$((a+b+5+d))will evaluate the expression between$((and))(including expanding all variables) and assign the result toe