Reputation: 13
I am getting a few of these errors in my bash script. I am new to the language. Any pointers?
#!/bin/bash
echo "Calculating the value V for all given values"
inflation=(0 0.03 0.05)
tax_rate=(0 0.28 0.35)
for I in inflation
do
for R in tax_rate
do
V=(4000*((1+0.07*(1-R))/(1+I))^10)
echo -n "$V "
done
done
This is my output:
Calculating the value V for all given values
./investment.sh: line 9: syntax error near unexpected token ('
./investment.sh: line 9:
V=(4000*((1+0.07*(1-R))/(1+I))^10)'
./investment.sh: line 12: syntax error near unexpected token done'
./investment.sh: line 12:
done'
Upvotes: 0
Views: 2533
Reputation: 185831
do
statements)bash
can't compute floating numbers itself, use bc [1] instead$(( ))
(( ))
forminflation=(0 0.03 0.05)
is an array, you can access it via "${inflation[@]}"
[1] bc
bc <<< "scale=2; (4000*((1+0.07*(1-$r))/(1+$i))^10)"
[2] Learn how to quote properly in shell, it's very important :
"Double quote" every literal that contains spaces/metacharacters and every expansion:
"$var"
,"$(command "$var")"
,"${array[@]}"
,"a & b"
. Use'single quotes'
for code or literal$'s: 'Costs $5 US'
,ssh host 'echo "$HOSTNAME"'
. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
Command Substitution: "$(cmd "foo bar")" causes the command 'cmd' to be executed with the argument 'foo bar' and "$(..)" will be replaced by the output. See http://mywiki.wooledge.org/BashFAQ/002 and http://mywiki.wooledge.org/CommandSubstitution
$((...))
is an arithmetic substitution. After doing the arithmetic, the whole thing is replaced by the value of the expression. See http://mywiki.wooledge.org/ArithmeticExpression
((...))
is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed.
#!/bin/bash
echo "Calculating the value v for all given values"
inflation=(0 0.03 0.05)
tax_rate=(0 0.28 0.35)
for i in "${inflation[@]}"; do
for r in "${tax_rate[@]}"; do
v="$(bc <<< "scale=2; (4000*((1+0.07*(1-$r))/(1+$i))^10)")"
echo -n "$v "
done
done
echo
Calculating the value v for all given values
7840.00 6480.00 5920.00 5360.00 4400.00 4000.00 4400.00 4000.00 3600.00
Upvotes: 1