Reputation: 203
I'm a Linux newbie and have copied a bash script that extracts values from a XML. I can echo the result of a calculation perfectly, but assigning this to a variable doesn't seem to work.
#!/bin/bash
IFS=$'\r\n' result=(`curl -s "http://xxx.xxx.xxx.xxx/smartmeter/modules" | \
xmlstarlet sel -I -t -m "/modules/module" \
-v "cumulative_logs/cumulative_log/period/measurement" -n \
-v "point_logs/point_log/period/measurement" -n | \
sed '/^$/d' `)
# uncomment for debug
echo "${result[0]}"*1000 |bc
gas=$(echo"${result[0]}"*1000 |bc)
echo "${result[0]}"*1000 |bc
Gives me the result I need, but I do not know how to assign it to a variable.
I tried with tick marks:
gas=\`echo"${result[0]}"*1000 |bc\`
And with $(
Can somebody point me in the right direction?
Upvotes: 20
Views: 41777
Reputation: 290525
There is no need to use bc
nor echo
. Simply use the arithmetic expansion $(( expression ))
for such operations:
gas=$(( ${result[0]} * 1000))
This allows the evaluation of an arithmetic expression and the substitution of the result.
Upvotes: 9
Reputation: 12130
If you want to use bc
anyway then
you can just use back ticks , why you are using the backslashes? this code works , I just tested.
gas=`echo ${result[0]}*1000 | bc`
Use one space after echo
and no space around * operator
Upvotes: 24