LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   expert help needed with bash and bc (http://www.linuxquestions.org/questions/programming-9/expert-help-needed-with-bash-and-bc-335430/)

karistouf 06-20-2005 11:36 AM

expert help needed with bash and bc
 
hello everybody, i m writting a little bash script to render animations with BMRT 2.6.

The script replace a Keyword by the mathematical value issued from the valuestrat and the valueend , on a certain number of images we want.

I have a main problem, as bash is not treating the float caculations is that:
- using bc I have all the time an input error when i give bc float variables
-only bc is the problem, when we enter integer there is no problem
-i tried the eof way to code wich give same result
-i want to have the less dependencies and want to arrive to work only thruth bash and bc only, that the script can be use from any Linux machine without having any special dependencies or libraries to load

-by the way I use convert from Image magick to chang tiff generated in png, to assembl them under blender in AVI file

Thanks for your help

Christoph



#!/bin/bash
#dependances: bc imagemagick
function CONCATANATE {
COUNTER=0
while [ $COUNTER -lt $NbImages ] ;
do
cat SEQ_$NomFichier/Temp_$NomFichier$COUNTER | sed s/ImageNum/$COUNTER/g | sed s/"FrameBegin 1"/"FrameBegin $COUNTER"/g | cat >> SEQ_$NomFichier/OUTPUT_CONCAT_$NomFichier
let COUNTER=COUNTER+1
done
echo Concatenation OK dans le fichier SEQ_$NomFichier/OUTPUT_CONCAT_$NomFichier
}


function REPLACE {
if [[ $VariableEnd -gt $VariableBegin ]]
then
CURVAR=$(echo "scale=6; (($VariableEnd - $VariableBegin)/$NbImages)*$COUNTER" |bc)
echo $Variable $CURVAR
fi
if [[ $VariableBegin -ge $VariableEnd ]]
then
CURVAR=$(echo "scale=6; ((($VariableBegin - $VariableEnd)/$NbImages)*($NbImages - $COUNTER))" |bc )
echo $Variable $CURVAR
fi

if [[ $ENTREE -le $NbVariables ]]
then
if [[ $ENTREE -eq 1 ]]
then
echo Image $COUNTER Replacement de $Variable
( cat SEQ_$NomFichier/BASE_$NomFichier | sed s/$Variable/$CURVAR/g ) > SEQ_$NomFichier/Temp_$NomFichier$COUNTER
else
if [[ $ENTREE -gt 1 ]]
then
( cat SEQ_$NomFichier/Temp_$NomFichier$COUNTER | sed s/$Variable/$CURVAR/g ) > SEQ_$NomFichier/Temp_$NomFichier$COUNTER_bis
cp SEQ_$NomFichier/Temp_$NomFichier$COUNTER_bis SEQ_$NomFichier/Temp_$NomFichier$COUNTER
rm SEQ_$NomFichier/Temp_$NomFichier$COUNTER_bis
fi
fi
fi


}


function RENDER {
rendrib SEQ_$NomFichier/OUTPUT_CONCAT_$NomFichier
}

function CONVERSION {
echo "conversion tif en png"
for i in 'ls *.tif'
do
echo conversion en cours $NomFichier.png ...
convert $i png: SEQ_$NomFichier/$NomFichier.png
echo Ok
done
}

function ENTREEVARIABLES {

while [ $ENTREE -le $NbVariables ] ;
do
echo "*** Entrer la variable a changer :"
read Variable
echo "Entrer Valeur de depart de $Variable"
read VariableBegin
echo "Entrer Valeur d arrivee de $Variable"
read VariableEnd
while [ $COUNTER -le $NbImages ] ;
do
REPLACE
let COUNTER=COUNTER+1
done
let ENTREE=ENTREE+1
COUNTER=0
done
while [ $COUNTER -le $NbImages ] ;
do
REPLACE
let COUNTER=COUNTER+1
done
}

export SHADERS=/usr/local/shaders
echo "Entrer le nom du fichier a traiter :"
read NomFichier
mkdir SEQ_$NomFichier
cp $NomFichier SEQ_$NomFichier/BASE_$NomFichier
echo "Entrer le nombre d images :"
read NbImages
echo "Entrer le nombre de variables :"
read NbVariables


#obligation d'initialiser les variables
COUNTER=0
ENTREE=1

ENTREEVARIABLES
CONCATANATE
RENDER
CONVERSION

echo OK
unset COUNTER
unset ENTREE
unset NbVariables
unset NbImages
unset NomFichier

Looking_Lost 06-20-2005 05:36 PM

I tried one of your bc calculations using constant values, it works without an error (no idea if it's the right numerical answer though). This would seem to suggest to me that one of the variables in the calculations doesn't contain a proper numberical value for bc and hasn't been set proper. Maybe something for you to explore, maybe not :)

Code:



#!/bin/bash

VariableEnd=200.456789
VariableBegin=399.433

COUNTER=20
NbImages=4

CURVAR=$(echo "scale=6; ((($VariableEnd -  $VariableBegin)/$NbImages) * ($NbImages - $COUNTER)) " | bc)

echo $CURVAR

#This Works okay

Code:


#!/bin/bash

VariableEnd=200.456789
VariableBegin=399.433

COUNTER=20
NbImages=""  #not a numerical value

CURVAR=$(echo "scale=6; ((($VariableEnd -  $VariableBegin)/$NbImages) * ($NbImages - $COUNTER)) " | bc)

echo $CURVAR


#This doesn't work Parse Error


karistouf 06-21-2005 02:45 AM

hi , and thanks for your interrest and help.

as this script is made for generating animation I will explain more precisely its utility and finality:

to create an animation , i use bmrt render machine wich generate tif

we say for example that our animation is at a frame rate of 25 images per second ( or 30 for usa or japan)

this value is an integer and never be a float value.


Imagine that the .rib file we will treat as in its code a sphere( rib and renderman language are declaration you can write in a vim editor or anything else):

Translate 2 -1.13 0
Sphere 360 ...

I first have made design / modeling thruth Ayam3d and then exported the file in rib language

This file will be treated by rendrib ( the renderer) and generate ONE image

To make animation I will first edit my rib file and change the value I want to affect by a KEYWORD:

Translate TX -1.13 0
Sphere 360...

Then I will call the script:
-script ask first wich file I want to treat
-then number of total images I want to generate ( if I want an animation of 2seconds it would be 50 images)
-and then ask how many wariables I want to change: say ONE ( we have created only one keyword)
-and finally at wich value at the begin ( first frame) it will be ; and wich value TX will be on last frame
-substitution is done then to replace TX by the value that is CURVAR
-images are generated

I agree with you that there is something wrong. I used the option scale with bc but also i tryed to scale at 6 and I tried with values start and end being for example 0.546821 and 3.514759. It stills receiving this as an error.

I still do not understand why, as read fonction rechoes the right value you will type as float.
untill now i did nt arrived to solve this problem. Wich is quite annoying, as float values are very used often in renderman object, and that if you produce 2 animations that are supposed to be edited together at the end in one avi file, you need to be precise.
( example: my ball will FIRST go to right, THEN go up)
By using Ayam 3d i have also a very precise tool thuth modeling to compose precisely the frame, and all arguments ( shaders, special funxctions) are in float.

Thanks for your help, and I hope to read you soon.

christoph

karistouf 06-21-2005 03:01 AM

still same troubles
 
okay, i made other test. By simplifing the script as you have done it works.
Also with any float value....

But I have errors when executing the script at line 15 and 20: [[: 5.989786: syntax error in expression (error token is ".989786")
this repeated as many curvar I have ( 5.989786 is one of the resuts of CURVAR)

Do I need to put a sleep or is there a problem of my definition of loops in my script ?

Thanks for your answer.
sincerely yours
christoph

Looking_Lost 06-21-2005 02:18 PM

I think it's the if statements bombing out because bash can only do integers.

You can try something like this:

#!/bin/bash

VariableEnd=100.3456
VariableBegin=99.4222

if [ $(echo "scale=6; if($VariableEnd > $VariableBegin) print "1" " | bc) ]; then
echo "Hello, there, dude"
fi

#The print "1" doesn't seem do anything apart from stopping bc complaing (no expert you see) although the if statement appears at
#first sight to evaluate correctly, don't ask me the technical reasons why :)

karistouf 06-22-2005 01:55 AM

its quite logical and i will make a try, the if statement is like in C++ condition that open an other form of logical way .
when you put a if ( like when you make a test with erors : if return=X then ...) you have a direct link to other underfunctions ( well i think its what i understood from previous coding)

Thanks, i keep in touch

sincerely
christoph


All times are GMT -5. The time now is 03:42 PM.