• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Bash help

nkk

Android Expert
So I am just learning bash, and I had to make a program that simulates the output of a valve controlled steam heater. My output is of a time (in this script just the counter of the while loop), three temperature readings, and a string indicating if the valve state was changed during that loop interation. The output is of the form:
Code:
int.int.int,int,string
 
which is equilivant to

timer,temp1,temp2,temp3.string to see if valve state was changed

Since this is just a random output generator (for testing of my analysis program, not for any real use), the temps are all just random numbers,

Here is the script:

Code:
#!/bin/bash

#some variables...
CLOSEVALVE='the valve was closed'
OPENVALVE='the valve was opened'
COUNTER=0






#make the necessary files

#delete the old file if it exists
if [ -e output.csv ]; then
rm output.csv
fi

#make a new one
touch output.csv

#Generate the actual output for a simulated 25000 loops
while [  $COUNTER -lt 25000 ]; do

#generate the random temps (three ofthem) 
# between 40 and 80
T_ONE=$[ ( $RANDOM % 41 )  + 40 ]
T_TWO=$[ ( $RANDOM % 41 )  + 40 ]
T_THREE=$[ ( $RANDOM % 41 )  + 40 ]
TOGGLE_VALVE=$[ ($RANDOM % 2 ) ]


#Echo the random values with commas to the output file (a CSV)
echo -ne $COUNTER >> output.csv
echo -ne ',' >> output.csv
echo -ne $T_ONE >> output.csv
echo -ne ',' >> output.csv        
echo -ne $T_TWO >> output.csv
echo -ne ',' >> output.csv
echo -ne $T_THREE >> output.csv



#check to see if should echo valve change string idicnator
if [ ( $TOGGLE_VALUE -eq 0 ) ]; then
echo -ne ',' >> output.csv
echo -ne $CLOSEVALVE >> output.csv
fi

if [ ( $TOGGLE_VALUE -eq 1 ) ]; then
echo -ne $OPENVALVE >> output.csv
fi


     #send in the newline character
     echo -ne '\n' >> output.csv

     #increment the counter
     let COUNTER=COUNTER+1 
 done

When I run it, I get:

Code:
'/home/myName/Dropbox/Workspaces/Heating-System-Programs/random_simulated_ouput_generator/randomInfoGenerator.sh' 

/home/myName/Dropbox/Workspaces/Heating-System-Programs/random_simulated_ouput_generator/randomInfoGenerator.sh: line 46: syntax error near unexpected token `$TOGGLE_VALUE'

/home/myName/Dropbox/Workspaces/Heating-System-Programs/random_simulated_ouput_generator/randomInfoGenerator.sh: line 46: `if [ ( $TOGGLE_VALUE -eq 0 ) ]; then'

So...can anyone help me? If you need any more info about the code (I thought the comments were enough to get the gist of it), please ask. You are doing me a huge favor.

Thanks
Nkk
 
Now, I've never tried anything with random numbers yet, but when I look at that output.csv file, it's empty. So that makes me think that the problem lies with those....

And debugging wasn't much help with this, it just showed the same error you got.

EDIT: @mrspeed
when I do that, I got a different error, something about -eq not being found...

Code:
./b1.sh: line 46: -eq: command not found
./b1.sh: line 51: -eq: command not found
 
Okay, I don't really know what is so different about out scripts, but mine isn't giving me an error...

Code:
#!/bin/bash

#
close='the valve was closed'
open='the valve was opened'
count=0

#
# checks for the output file
# removes the old one
#

if [ -e output1.csv ]
then
    rm output1.csv
fi

while [ $count -lt 5 ]
do

T_one=$[ ( $RANDOM % 41 ) + 40 ]
T_two=$[ ( $RANDOM % 41 ) + 40 ]
T_three=$[ ( $RANDOM % 41 ) + 40 ]
toggle=$[ ( $RANDOM % 2 ) ]

# echo lines to output file
echo -ne $count,$T_one,$T_two,$T_three >> output1.csv

if [ $toggle -eq 0 ]
then
    echo -ne ',' >> output1.csv
    echo -ne $close >> output1.csv
else
    echo -ne ',' >> output1.csv
    echo -ne $open >> output1.csv
fi

echo -ne '\n' >> output1.csv

let count=$count+1
done

my output file looks like this:
Code:
0,62,40,41,the valve was closed
1,44,63,78,the valve was opened
2,72,47,61,the valve was closed
3,66,68,48,the valve was closed
4,63,67,49,the valve was closed
 
  • Like
Reactions: nkk
Okay, I don't really know what is so different about out scripts, but mine isn't giving me an error...

Code:
#!/bin/bash

#
close='the valve was closed'
open='the valve was opened'
count=0

#
# checks for the output file
# removes the old one
#

if [ -e output1.csv ]
then
    rm output1.csv
fi

while [ $count -lt 5 ]
do

T_one=$[ ( $RANDOM % 41 ) + 40 ]
T_two=$[ ( $RANDOM % 41 ) + 40 ]
T_three=$[ ( $RANDOM % 41 ) + 40 ]
toggle=$[ ( $RANDOM % 2 ) ]

# echo lines to output file
echo -ne $count,$T_one,$T_two,$T_three >> output1.csv

if [ $toggle -eq 0 ]
then
    echo -ne ',' >> output1.csv
    echo -ne $close >> output1.csv
else
    echo -ne ',' >> output1.csv
    echo -ne $open >> output1.csv
fi

echo -ne '\n' >> output1.csv

let count=$count+1
done

my output file looks like this:
Code:
0,62,40,41,the valve was closed
1,44,63,78,the valve was opened
2,72,47,61,the valve was closed
3,66,68,48,the valve was closed
4,63,67,49,the valve was closed

Thanks for the help. When I had if [ $var -eq 1] I got some error about expecting a unary operator. Let me get back to a full Linux machine (forced onto windows for stupid software) and try it.

Thanks again
Nkk
 
I got that error too for a while, not entirely sure why.

I don't even know what I did to get rid of it, but last I checked it was gone...
;)
 
Back
Top Bottom