[opensuse] bash question - why calling 'calc' from script causes "bg" is undefined



Bash gurus,

Here is one I don't understand. I needed a simple script to part gtkrc and convert the r,g,b (0.0-1.0) values to (0-255). So I decided to use 'calc' to get the floating point values. The script was:

#!/bin/bash

while read l; do
[[ $l =~ , ]] && [[ ${l:0:1} != [#] ]] && {
rgb=${l##*[{] }
rgb=${rgb%\ \}*}
rgb=${rgb//,}
r=${rgb%% *}
g=${rgb% *}
g=${g#* }
b=${rgb##* }
_r=$(calc -p "255 * $r")
_g=$(calc -p "255 * $g")
_b=$(calc -p "255 * $b")
echo "${rgb} => r:$r g:$g b:$b => r:$_r g:$_g b:$_b";
}
done < gtkrc-file.txt

The sample gtkrc file is included at the end of the message. Running the script resulted in the error:

15:38 providence:~/cnf/kde3> sh parsegtkrc.sh
"bg" is undefined

0.125 0.129 0.149 => r:0.125 g:0.129 b:0.149 => r:31.875 g:32.895 b:37.995

Huh? It worked, but only for the first value and threw a "bg" is undefined error?? OK, lets try it this way:

<snip>
_r=$(echo "255*$r" | calc -p)
_g=$(echo "255*$g" | calc -p)
_b=$(echo "255*$b" | calc -p)
<snip>

15:46 providence:~/cnf/kde3> sh parsegtkrc.sh
0.125 0.129 0.149 => r:0.125 g:0.129 b:0.149 => r:31.875 g:32.895 b:37.995
0.216 0.286 0.431 => r:0.216 g:0.286 b:0.431 => r:55.080 g:72.930 b:109.905
0.125 0.129 0.149 => r:0.125 g:0.129 b:0.149 => r:31.875 g:32.895 b:37.995
<snip -- All good!>

Why is my first invocation of calc not correct? What is the "bg" is undefined error? This is really bewildering because I can do:

#!/bin/bash

for((i=0;i<10;i++)); do
mult=$((i+5))
num=$(calc -p "$mult * 5.1")
echo "num: $num"
done

So I don't see why the error was generated in the first place. What say the experts?

-------- sample gtkrc-file.txt ----------
# created by KDE, Mon May 21 13:27:49 2012
#
# If you do not want KDE to override your GTK settings, select
# Appearance & Themes -> Colors in the Control Center and disable the checkbox
# "Apply colors to non-KDE applications"
#
#
style "default"
{
bg[NORMAL] = { 0.125, 0.129, 0.149 }
bg[SELECTED] = { 0.216, 0.286, 0.431 }
bg[INSENSITIVE] = { 0.125, 0.129, 0.149 }
bg[ACTIVE] = { 0.102, 0.106, 0.122 }
bg[PRELIGHT] = { 0.125, 0.129, 0.149 }

base[NORMAL] = { 0.035, 0.051, 0.078 }
base[SELECTED] = { 0.216, 0.286, 0.431 }
base[INSENSITIVE] = { 0.125, 0.129, 0.149 }
base[ACTIVE] = { 0.216, 0.286, 0.431 }
base[PRELIGHT] = { 0.216, 0.286, 0.431 }

text[NORMAL] = { 0.882, 0.835, 0.702 }
text[SELECTED] = { 1.000, 1.000, 0.776 }
text[INSENSITIVE] = { 0.102, 0.106, 0.122 }
text[ACTIVE] = { 1.000, 1.000, 0.776 }
text[PRELIGHT] = { 1.000, 1.000, 0.776 }

fg[NORMAL] = { 0.918, 0.867, 0.729 }
fg[SELECTED] = { 1.000, 1.000, 0.776 }
fg[INSENSITIVE] = { 0.102, 0.106, 0.122 }
fg[ACTIVE] = { 0.918, 0.867, 0.729 }
fg[PRELIGHT] = { 0.918, 0.867, 0.729 }
}

class "*" style "default"

gtk-alternative-button-order = 1

style "ToolTip"
{
bg[NORMAL] = { 1.000, 1.000, 0.863 }
base[NORMAL] = { 1.000, 1.000, 0.863 }
text[NORMAL] = { 0.000, 0.000, 0.000 }
fg[NORMAL] = { 0.000, 0.000, 0.000 }
}

widget "gtk-tooltip" style "ToolTip"
widget "gtk-tooltips" style "ToolTip"

style "MenuItem"
{
bg[PRELIGHT] = { 0.216, 0.286, 0.431 }
fg[PRELIGHT] = { 1.000, 1.000, 0.776 }
}

class "*MenuItem" style "MenuItem"

Thanks for any insight you can provide. Yep, the '_r=$(echo "255*$r" | bc)' style also works fine as well.

--
David C. Rankin, J.D.,P.E.
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx



Relevant Pages