Code: Select all
g='-0.028 in'
if g<-.02 then h='Falling'
if g>.02 then h='Rising'
else h='Steady"Can someone explain where I'm going wrong?
Thanks for any help.
Code: Select all
g='-0.028 in'
if g<-.02 then h='Falling'
if g>.02 then h='Rising'
else h='Steady"Bash can only evaluate integers so your decimal point will fail it, as will having " in" at the end. To compare integers you dosolo2500 wrote:Im trying to write a shell scrip that does this:I'm not a programer... Just a hack!Code: Select all
g='-0.028 in' if g<-.02 then h='Falling' if g>.02 then h='Rising' else h='Steady"
Can someone explain where I'm going wrong?
Thanks for any help.
Code: Select all
if [ arg1 OP arg2 ]; then
... what you want to happen when true
else
... what you want to happen when false
fi
Code: Select all
g=2
echo $g
Code: Select all
#!/bin/bash
g=2
if [ $g -lt 2 ]; then
h="falling"
else
if [ $g -gt 2 ]; then
h="rising"
else
h="steady"
fi
fi
echo $h
Code: Select all
sudo apt-get update
sudo apt-get install bc
Code: Select all
#!/bin/bash
g='-0.028'
if [ $(echo "$g < -0.02" | bc) -eq 1 ]; then
h="falling"
else
if [ $(echo "$g > 0.02" | bc) -eq 1 ]; then
h="rising"
else
h="steady"
fi
fi
echo $h
Paeryn wrote:Bash can only evaluate integers so your decimal point will fail it, as will having " in" at the end. To compare integers you dosolo2500 wrote:Im trying to write a shell scrip that does this:I'm not a programer... Just a hack!Code: Select all
g='-0.028 in' if g<-.02 then h='Falling' if g>.02 then h='Rising' else h='Steady"
Can someone explain where I'm going wrong?
Thanks for any help.where OP is one of -eq (equal) -ne (not equal) -lt (less than) -gt (greater than) -le (less than or equal) -ge (greater than or equal).Code: Select all
if [ arg1 OP arg2 ]; then ... what you want to happen when true else ... what you want to happen when false fi
Note the spaces around the square brackets - they are important!
You have to prefix a variable with $ when you want to read it's valueCode: Select all
g=2 echo $gIf you want to compare floating point numbers you'll have to pipe the test to bc for it to do the test (bc returns 1 if the test is true). Although bc may not be installed by default, it can be installed with :-Code: Select all
#!/bin/bash g=2 if [ $g -lt 2 ]; then h="falling" else if [ $g -gt 2 ]; then h="rising" else h="steady" fi fi echo $hFinal code :-Code: Select all
sudo apt-get update sudo apt-get install bcCode: Select all
#!/bin/bash g='-0.028' if [ $(echo "$g < -0.02" | bc) -eq 1 ]; then h="falling" else if [ $(echo "$g > 0.02" | bc) -eq 1 ]; then h="rising" else h="steady" fi fi echo $h
Code: Select all
echo $g
echo "dddddddddddddddddd" #debug marker
if [ $(echo "$g < -0.02" | bc) -eq 1 ]; then
h="Falling"
else
if [ $(echo "$g > 0.02" | bc) -eq 1 ]; then
h="Rising"
else
h="Steady"
fi
fi
echo $h
Code: Select all
0.026 inHg
dddddddddddddddddd
(standard_in) 1: syntax error
(standard_in) 1: illegal character: H
./t1.sh: line 28: [: -.02: integer expression expected
(standard_in) 1: syntax error
(standard_in) 1: illegal character: H
./t1.sh: line 31: [: .02: integer expression expected
Steady
Yes, as I put at the start,solo2500 wrote:I think there is a slight syntax error? This is the code:and this is what I get when I run it:Code: Select all
echo $g echo "dddddddddddddddddd" #debug marker if [ $(echo "$g < -0.02" | bc) -eq 1 ]; then h="Falling" else if [ $(echo "$g > 0.02" | bc) -eq 1 ]; then h="Rising" else h="Steady" fi fi echo $hDo I need to strip off the inHg? I can do that. Edit: I just did that and still get the same return?Code: Select all
0.026 inHg dddddddddddddddddd (standard_in) 1: syntax error (standard_in) 1: illegal character: H ./t1.sh: line 28: [: -.02: integer expression expected (standard_in) 1: syntax error (standard_in) 1: illegal character: H ./t1.sh: line 31: [: .02: integer expression expected Steady
It needs to be just a number. If the variable contains a number followed by space followed by anything else you can cut it down to everything before the space with ${g%% *}Paeryn wrote:Bash can only evaluate integers so your decimal point will fail it, as will having " in" at the end.
Code: Select all
g="0.026 inHg"
value=${g%% *}
if [ $(echo "$value < 0.02" | bc) -eq 1 ]; then
Code: Select all
g=${g%% *}Code: Select all
pi@rpi3:~ $ g="0.026 inHg" ./tm.sh
0.026 inHg
dddddddddddddddddd
Rising
Code: Select all
value=${g%% *}
if [ $(echo "$value < 0.02" | bc) -eq 1 ]; then
if [ $(echo "$value < -0.02" | bc) -eq 1 ]; then
h="Falling"
else
if [ $(echo "$value > 0.02" | bc) -eq 1 ]; then
h="Rising"
else
h="Steady"
fi
fi
echo $h
Code: Select all
./t1.sh: line 83: syntax error: unexpected end of file
You've got two if lines together at the top so you are still in that first if block when you exit.solo2500 wrote:ok... I now have:which seems to have solved a few of the errors but now I'm getting:Code: Select all
value=${g%% *} if [ $(echo "$value < 0.02" | bc) -eq 1 ]; then if [ $(echo "$value < -0.02" | bc) -eq 1 ]; then h="Falling" else if [ $(echo "$value > 0.02" | bc) -eq 1 ]; then h="Rising" else h="Steady" fi fi echo $h?Code: Select all
./t1.sh: line 83: syntax error: unexpected end of file
Code: Select all
value=${g%%[^-.[:digit:]]*}
Another idea might be to ask why the numbers that are tested can't just be 1000 times bigger and not have decimals at all.solo2500 wrote:Im trying to write a shell scrip that does this:Code: Select all
g='-0.028 in' if g<-.02 then h='Falling' if g>.02 then h='Rising' else h='Steady"
perhaps take a look @ https://learnxinyminutes.com/docs/bash/solo2500 wrote: Can someone explain where I'm going wrong?