LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2017, 01:31 PM   #1
bobpick
LQ Newbie
 
Registered: Dec 2017
Posts: 6

Rep: Reputation: Disabled
If/then test not going well.


I am trying my darndest to get this script to work. In essence, it tests values every 30 minutes, with price0 being right now, price30 being 30 minutes ago, and price60 is, as you might guess, 60 minutes ago.

Here is the code section:

Code:
price0=45.020
price30=42.00295
price60=40.0003834
if echo " $price0 > $price30 " | bc -l ; then
	echo $currency " meets 1/2 of the requirements"
	rm current+$currency.tx
	echo $price0, $price30, $price60, "0 and 30 and 60"
	if echo " $price30 > $price60 " | bc -l ; then
			echo $currency "meets the other half of the requirements"
			echo $currency >> BTC.txt
			sed -i '/-/d' BTC.txt
			#rm current+$currency.tx
			else
			echo $currency " is not a winner. "
	fi
	else
	echo "Nope... 1/2 didn't work"
fi
This is the output I get when conditions are 0>30>60

I have the data...
1
USDT_BTC meets 1/2 of the requirements
45.020, 42.00295, 40.0003834, 0 and 30 and 60
1
USDT_BTC meets the other half of the requirements

And this is what I'm looking for. But when I swap 0 and 30, this is what I'm getting:

when 0<30>60

I have the data...
0
USDT_BTC meets 1/2 of the requirements
42.00295, 45.020, 40.0003834, 0 and 30 and 60
1
USDT_BTC meets the other half of the requirements

I see that the 1 and 0 represent that the test was true or false. In the first test should be false - but it seems to think it is true...

This is on a Raspberry Pi 3B, running Raspbian.

I'm sure my problem is something simple.. I just can't see it.
 
Old 12-23-2017, 01:52 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,467

Rep: Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274
Welcome to LinuxQuestions.

How are you running your script? What shell i.e. bash etc.

Code:
wrong answer see below.

Last edited by michaelk; 12-23-2017 at 03:20 PM.
 
Old 12-23-2017, 02:00 PM   #3
bobpick
LQ Newbie
 
Registered: Dec 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
I'm using this:
#!/bin/sh

I just omitted the first half of the script as it was irrelevant.

When I change it to what you have, I get this for output:
./balance.sh: 45: ./balance.sh: 42.00295 > 45.020 : not found

So I use the echo command.
 
Old 12-23-2017, 02:04 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,467

Rep: Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274
Code:
if [ $price0 > $price30 ]; then
  ....
else
  echo "Nope... 1/2 didn't work"
fi
You have not posted that part of the script that calls balance.sh

Last edited by michaelk; 12-23-2017 at 02:12 PM.
 
Old 12-23-2017, 02:14 PM   #5
bobpick
LQ Newbie
 
Registered: Dec 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
This is what I get:

I have the data...
./balance.sh: 45: [: missing ]
File ] is unavailable.
Nope... 1/2 didn't work

And here are the changes:

Code:
if [ " $price0 > $price30 " | bc -l ]; then
	echo $currency " meets 1/2 of the requirements"
	rm current+$currency.tx
	echo $price0, $price30, $price60, "0 and 30 and 60"
	if [ " $price30 > $price60 " | bc -l ] ; then
			echo $currency "meets the other half of the requirements"
			echo $currency >> BTC.txt
			sed -i '/-/d' BTC.txt
			#rm current+$currency.tx
			else
			echo $currency " is not a winner. "
	fi
	else
	echo "Nope... 1/2 didn't work"
fi

This has been killing me for the past few days.

Last edited by bobpick; 12-23-2017 at 02:21 PM.
 
Old 12-23-2017, 02:19 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,467

Rep: Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274
Please use code tags.

Code:
ignore see below

Last edited by michaelk; 12-23-2017 at 02:58 PM.
 
1 members found this post helpful.
Old 12-23-2017, 02:28 PM   #7
bobpick
LQ Newbie
 
Registered: Dec 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
When I ran it, I got this:

I have the data...
USDT_BTC meets 1/2 of the requirements
45.020, 42.00295, 40.0003834, 0 and 30 and 60
USDT_BTC meets the other half of the requirements

But when I changed the data, it still considered it to be true.

price30=45.020
price0=42.00295
price60=40.0003834

I have the data...
USDT_BTC meets 1/2 of the requirements
42.00295, 45.020, 40.0003834, 0 and 30 and 60
USDT_BTC meets the other half of the requirements
 
Old 12-23-2017, 02:53 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,467

Rep: Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274
Argh.... sh only does integer math which is why you need bc. My brain isn't working...

Your first attempt was actually more correct then mine.

Code:
if [ $(echo "$price0 > $price30" | bc) -eq 1 ]; then
  echo $currency " meets 1/2 of the requirements"
  rm current+$currency.tx
  echo $price0, $price30, $price60, "0 and 30 and 60"
  if   [ $(echo "$price30 > $price60" | bc) -eq 1 ]; then
     echo $currency "meets the other half of the requirements"
     echo $currency >> BTC.txt
     sed -i '/-/d' BTC.txt
     #rm current+$currency.tx
  else
     echo " is not a winner. "
  fi
else
  echo "Nope... 1/2 didn't work"
fi

Last edited by michaelk; 12-23-2017 at 02:59 PM.
 
Old 12-23-2017, 03:03 PM   #9
bobpick
LQ Newbie
 
Registered: Dec 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Argh.... sh only does integer math which is why you need bc. My brain isn't working...

Your first attempt was actually more correct then mine.
What did I tell ya! Thanks. It works. I GREATLY appreciate the help.

Now, why was it failing? I left out the -eq 1 statement?
 
Old 12-23-2017, 03:27 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,467

Rep: Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274Reputation: 2274
That is one reason.

Code:
if echo " $price0 > $price30 " | bc -l ; then
vs
Code:
if [ $(echo "$price0 > $price30" | bc) -eq 1 ];
The $() in $(echo "$price0 > $price30") is called command substitution and $(echo "$price0 > $price30") and returns the value of 1 and -eq evaluates the expression if it equals 1 however in your code if evaluates to just a string which is always true.

Last edited by michaelk; 12-23-2017 at 03:28 PM.
 
Old 12-23-2017, 03:36 PM   #11
bobpick
LQ Newbie
 
Registered: Dec 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks again. I do appreciate the time you took.
 
Old 12-23-2017, 05:17 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,614

Rep: Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333
Code:
#start values
userx@slackwhere:~
$ price0=10
userx@slackwhere:~
$ price1=20

userx@slackwhere:~
$ [[ $(echo "$price0 > $price1" | bc) -eq 1 ]] && echo "true" || echo "false"
false

#changed value
userx@slackwhere:~
$ price0=30
userx@slackwhere:~
$ [[ $(echo "$price0 > $price1" | bc) -eq 1 ]] && echo "true" || echo "false"
true

userx@slackwhere:~
$ echo "$price0 > $price1" | bc
1

#changed value to  price0 less than price1
userx@slackwhere:~
$ price0=5 
userx@slackwhere:~
$ echo "$price0 > $price1" | bc
0

Last edited by BW-userx; 12-23-2017 at 05:20 PM.
 
Old 12-23-2017, 05:58 PM   #13
shinobi59
Member
 
Registered: Oct 2004
Location: Dimension X
Distribution: All
Posts: 59

Rep: Reputation: 15
Good response BW. You found the crux of the problem.

Last edited by shinobi59; 12-23-2017 at 06:11 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Load test, boundary test & stress test for USB EHCI/xHCI driver rama_toshiba Linux - Kernel 5 02-29-2012 02:43 PM
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration