solo2500
Posts: 86
Joined: Sat Jul 09, 2016 12:38 am

if then else in shell script

Mon Nov 21, 2016 2:19 am

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"
I'm not a programer... Just a hack!
Can someone explain where I'm going wrong?
Thanks for any help.
I'm a total novice, non-programer (...basically a hack.)

User avatar
Paeryn
Posts: 1841
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: if then else in shell script

Mon Nov 21, 2016 3:30 am

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"
I'm not a programer... Just a hack!
Can someone explain where I'm going wrong?
Thanks for any help.
Bash can only evaluate integers so your decimal point will fail it, as will having " in" at the end. To compare integers you do

Code: Select all

if [ arg1 OP arg2 ]; then
    ... what you want to happen when true
else
    ... what you want to happen when false
fi
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).
Note the spaces around the square brackets - they are important!
You have to prefix a variable with $ when you want to read it's value

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
If 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

sudo apt-get update
sudo apt-get install bc
Final code :-

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
She who travels light — forgot something.

solo2500
Posts: 86
Joined: Sat Jul 09, 2016 12:38 am

Re: if then else in shell script

Mon Nov 21, 2016 2:27 pm

Paeryn,
THANK YOU for taking the time to reply especially with such detail!
Again... Thank you!


Paeryn wrote:
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"
I'm not a programer... Just a hack!
Can someone explain where I'm going wrong?
Thanks for any help.
Bash can only evaluate integers so your decimal point will fail it, as will having " in" at the end. To compare integers you do

Code: Select all

if [ arg1 OP arg2 ]; then
    ... what you want to happen when true
else
    ... what you want to happen when false
fi
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).
Note the spaces around the square brackets - they are important!
You have to prefix a variable with $ when you want to read it's value

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
If 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

sudo apt-get update
sudo apt-get install bc
Final code :-

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
I'm a total novice, non-programer (...basically a hack.)

solo2500
Posts: 86
Joined: Sat Jul 09, 2016 12:38 am

Re: if then else in shell script

Mon Nov 21, 2016 3:17 pm

I think there is a slight syntax error? This is the code:

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
 
and this is what I get when I run it:

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
  
Do I need to strip off the inHg? I can do that. Edit: I just did that and still get the same return?
I'm a total novice, non-programer (...basically a hack.)

User avatar
Paeryn
Posts: 1841
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: if then else in shell script

Mon Nov 21, 2016 3:43 pm

solo2500 wrote:I think there is a slight syntax error? This is the code:

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
 
and this is what I get when I run it:

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
  
Do I need to strip off the inHg? I can do that. Edit: I just did that and still get the same return?
Yes, as I put at the start,
Paeryn wrote:Bash can only evaluate integers so your decimal point will fail it, as will having " in" at the end.
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%% *}
The %% tells bash to strip off the longest matching string of " *" which is a space followed by any number of characters. This results in a string up to (but not including) the first space.

Code: Select all

g="0.026 inHg"
value=${g%% *}
if [ $(echo "$value < 0.02" | bc) -eq 1 ]; then
Using your code, but with

Code: Select all

g=${g%% *}
added just after the echo $g line to strip the extra text from the variable...

Code: Select all

pi@rpi3:~ $ g="0.026 inHg" ./tm.sh
0.026 inHg
dddddddddddddddddd
Rising
She who travels light — forgot something.

solo2500
Posts: 86
Joined: Sat Jul 09, 2016 12:38 am

Re: if then else in shell script

Mon Nov 21, 2016 4:02 pm

ok... I now have:

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
 
which seems to have solved a few of the errors but now I'm getting:

Code: Select all

 ./t1.sh: line 83: syntax error: unexpected end of file
 
?
I'm a total novice, non-programer (...basically a hack.)

User avatar
Paeryn
Posts: 1841
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: if then else in shell script

Mon Nov 21, 2016 4:15 pm

solo2500 wrote:ok... I now have:

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
 
which seems to have solved a few of the errors but now I'm getting:

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.
As you have it the third if value > 0.02 can never be true as is it in the block that is only run if value < 0.02

I thought afterwards too, if you want to make sure it works if a string directly follows the number without any space, a better pattern would be

Code: Select all

value=${g%%[^-.[:digit:]]*}
That strips off everything starting from the first character that isn't a number, . or -
She who travels light — forgot something.

solo2500
Posts: 86
Joined: Sat Jul 09, 2016 12:38 am

Re: if then else in shell script

Mon Nov 21, 2016 4:25 pm

Oops! my bad!
I think I have been stating at the screen to long.
Once again thank you so much for all your help!
Cheers!
I'm a total novice, non-programer (...basically a hack.)

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: if then else in shell script

Mon Nov 21, 2016 7:49 pm

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"
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.

User avatar
r3d4
Posts: 924
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: if then else in shell script

Sat Jul 15, 2017 2:31 pm

solo2500 wrote: Can someone explain where I'm going wrong?
perhaps take a look @ https://learnxinyminutes.com/docs/bash/

also
read the feild manual ;)
or
at least
grep it a bit
https://www.gnu.org/software/bash/manual/bash.txt
:)
manual/bash.html#Conditional-Constructs

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 21 guests