LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-26-2010, 09:16 AM   #16
ydrol
LQ Newbie
 
Registered: Oct 2005
Posts: 3

Rep: Reputation: 0

'bc' mentioned 2005 and 2008
Only issue is
Quote:
# echo "2.2 > 1.1" | bc
/bin/sh: bc: not found
Hence other discussion.

Last edited by ydrol; 04-26-2010 at 09:46 AM.
 
Old 04-26-2010, 10:21 AM   #17
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Which distro are you using, that doesn't have bc? Or is your $PATH set OK? On Slackware 13.0:
Code:
c@CW8:~$ type bc
bc is /usr/bin/bc
 
Old 04-26-2010, 10:25 AM   #18
ydrol
LQ Newbie
 
Registered: Oct 2005
Posts: 3

Rep: Reputation: 0
as mentioned here

"eg many small devices running busybox"

# uname -a
Linux hdx 2.6.15-sigma #953 PREEMPT Wed Jun 3 21:49:15 CST 2009 mips GNU/Linux

# busybox
BusyBox v1.14.1 (2009-06-21 11:33:51 CEST) multi-call binary
Copyright (C) 1998-2008 Erik Andersen, Rob Landley, Denys Vlasenko
and others. Licensed under GPLv2.
See source distribution for full notice.

Usage: busybox [function] [arguments]...
or: function [arguments]...

BusyBox is a multi-call binary that combines many common Unix
utilities into a single executable. Most people will create a
link to busybox for each function they wish to use and BusyBox
will act like whatever it was invoked as!

Currently defined functions:
[, [[, addgroup, adduser, adjtimex, ar, arp, arping, ash,
awk, basename, bbconfig, blkid, brctl, bunzip2, bzcat, bzip2,
cal, cat, catv, chat, chattr, chgrp, chmod, chown, chpasswd,
chpst, chroot, chrt, chvt, cksum, clear, cmp, comm, cp,
cpio, crond, crontab, cryptpw, cttyhack, cut, date, dc,
dd, deallocvt, delgroup, deluser, depmod, devmem, df, dhcprelay,
diff, dirname, dmesg, dnsd, dos2unix, dpkg, dpkg-deb, du,
dumpkmap, dumpleases, echo, ed, egrep, eject, env, envdir,
envuidgid, ether-wake, expand, expr, fakeidentd, false,
fbset, fbsplash, fdflush, fdformat, fdisk, fgrep, find,
findfs, fold, free, freeramdisk, fsck, fsck.minix, ftpd,
ftpget, ftpput, fuser, getopt, getty, grep, gunzip, gzip,
halt, hd, hdparm, head, hexdump, hostid, hostname, httpd,
hush, hwclock, id, ifconfig, ifdown, ifenslave, ifup, inetd,
init, insmod, install, ip, ipaddr, ipcalc, ipcrm, ipcs,
iplink, iproute, iprule, iptunnel, kbd_mode, kill, killall,
killall5, klogd, last, length, less, linux32, linux64, linuxrc,
ln, loadfont, loadkmap, logger, login, logname, logread,
losetup, lpd, lpq, lpr, ls, lsattr, lsmod, lzmacat, makedevs,
makemime, man, md5sum, mdev, mesg, microcom, mkdir, mkdosfs,
mkfifo, mkfs.minix, mkfs.vfat, mknod, mkpasswd, mkswap,
mktemp, modprobe, more, mount, mountpoint, mt, mv, nameif,
nc, netstat, nice, nmeter, nohup, nslookup, od, openvt,
passwd, patch, pgrep, pidof, ping, ping6, pipe_progress,
pivot_root, pkill, popmaildir, poweroff, printenv, printf,
ps, pscan, pwd, raidautorun, rdate, rdev, readahead, readlink,
readprofile, realpath, reboot, reformime, renice, reset,
resize, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake,
run-parts, runlevel, runsv, runsvdir, rx, script, sed, sendmail,
seq, setarch, setconsole, setfont, setkeycodes, setlogcons,
setsid, setuidgid, sh, sha1sum, sha256sum, sha512sum, showkey,
slattach, sleep, softlimit, sort, split, start-stop-daemon,
stat, strings, stty, su, sulogin, sum, sv, svlogd, swapoff,
swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar,
tcpsvd, tee, telnet, telnetd, test, tftp, tftpd, time, timeout,
top, touch, tr, traceroute, true, tty, ttysize, tunctl,
udhcpc, udhcpd, udpsvd, umount, uname, uncompress, unexpand,
uniq, unix2dos, unlzma, unzip, uptime, usleep, uudecode,
uuencode, vconfig, vi, vlock, watch, watchdog, wc, wget,
which, who, whoami, xargs, yes, zcat, zcip
 
Old 04-26-2010, 10:59 AM   #19
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ydrol View Post
as mentioned here
Sorry -- I missed that
 
Old 11-05-2010, 02:34 AM   #20
frater
Member
 
Registered: Jul 2008
Posts: 121

Rep: Reputation: 23
Although some of the examples given in this thread do work (not all), I always strive to use syntax that is as human readable as possible.

Code:
x=3.3
y=3.2
if expr $x \> $y >/dev/null ; then
 echo "the value of x ($x) is greater than y ($y)"
else
 echo "the value of x ($x) is NOT greater than y ($y)"
fi
so it's as simple as this:
Code:
x=3.3
y=3.2
expr $x \> $y >/dev/null && echo "$x > $y"
And even busybox supports it:
Code:
busybox | grep -o expr
expr
Alas `expr` doesn't support a '-q' (quiet) so we need to pipe the output to /dev/null and only use the errorlevel it returns

Last edited by frater; 11-06-2010 at 05:58 AM.
 
Old 11-30-2010, 02:24 PM   #21
VinPhend
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Thumbs down

I am happy with
Quote:
Originally Posted by Galogen View Post
compare_result=`echo "2.2 > 1.1" | bc`
if [ $compare_result ]; then
echo 1
fi
My full script:
Code:
#!/bin/bash 
#Select group of plots by part of filename.
outputdir="last-selected"

#SEARCH="spacer:+0.095"
SEARCH="WG-width:0.190"
echo $SEARCH
ls *.png | grep $SEARCH > list.txt
rm -r $outputdir
mkdir $outputdir
mkdir $SEARCH
while IFS= read -r file
    do
    echo $file
    cp $file $outputdir
    done < "list.txt"  
cd $outputdir

#remove visually similar images
ls *.png | grep $SEARCH > listi.txt
ls *.png | grep $SEARCH > listj.txt
while IFS= read -r i;    do
    while IFS= read -r j; do
        #distanse between images in similarity space, float number
	result=$(puzzle-diff $i $j) 
	threshold=0.2
        #if not the same
	if [ "$result" != "0" ]
	then
	    check=`echo "$result<$threshold" | bc`
	    if [ "$check"  !=  "0" ]
	    then
                #remove similar
		rm $j
		ls *.png  > listi.txt
		ls *.png  > listj.txt
	    fi
	fi
    done < "listi.txt"  
done < "listj.txt"  
cd ../
cp -r $outputdir/*.png $SEARCH
 
Old 11-30-2010, 07:44 PM   #22
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
I'm happy you are happy

Threads can be marked SOLVED using the Thread Tools menu.
 
Old 07-20-2011, 04:16 PM   #23
bytecode
LQ Newbie
 
Registered: Oct 2009
Location: Cornwall/Devon
Distribution: *Bunty/Debian/Fedora/Gentoo/Mandriva
Posts: 3

Rep: Reputation: 1
For those stumbling upon this thread in the future:

Actually, it's even simpler than Fraters excellent solution if using BASH, as there is no need for "expr" - provided that:
1) you remember to ESCAPE the "<" or ">" operators with a slash "\"
and
2) the length of the floats is the same, e.g. "4.0 \< 5.0" or "40.5" \< "50.5" as it'll still be a string compare... (Otherwise you will need to use expr, as "4.0 \< 60.3"

Code:
byte@bluepoolinternet.com:/home/byte$ if [ 4.0 \< 5.0 ]; then echo TRUE; else echo FALSE; fi
TRUE
byte@bluepoolinternet.com:/home/byte$ if [ 6.0 \< 5.0 ]; then echo TRUE; else echo FALSE; fi
FALSE
It can get quite interesting with string datatypes...

If anyone is wondering why the original response from Moneycat failed:

Quote:
Originally Posted by MoneyCat View Post
This is because the -lt, -gt, -le, -ge, comparisons are only designed for integers. Try using these operators: >, <, >=, <=
It is because by default, in BASH, the >, < operators are RE-DIRECTION Operators, for redirecting output to files etc. so to use them in the test expression, you have to escape them, otherwise you get the confusing error re no such file or directory:

Code:
byte@bluepoolinternet.com:/home/byte$ if [ 4 < 5 ]; then echo TRUE; fi
bash: 5: No such file or directory
as the BASH interpreter is making the natural assumption that the "5" is a file, and would also have assumed that the "4" was a file also.
Obviously, once you realise that it's a real "D'oh!" moment ;-)


Quote:
Originally Posted by frater View Post
Although some of the examples given in this thread do work (not all), I always strive to use syntax that is as human readable as possible.

Code:
x=3.3
y=3.2
if expr $x \> $y >/dev/null ; then
 echo "the value of x ($x) is greater than y ($y)"
else
 echo "the value of x ($x) is NOT greater than y ($y)"
fi
so it's as simple as this:
Code:
x=3.3
y=3.2
expr $x \> $y >/dev/null && echo "$x > $y"
And even busybox supports it:
Code:
busybox | grep -o expr
expr
Alas `expr` doesn't support a '-q' (quiet) so we need to pipe the output to /dev/null and only use the errorlevel it returns

Last edited by bytecode; 07-20-2011 at 04:40 PM. Reason: incomplete explanation.
 
1 members found this post helpful.
Old 07-20-2011, 08:32 PM   #24
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Strictly speaking, '< > = == ... ' are string operators, '-eq -ne -lt ...' are numeric operators (in a test statement) in bash, ksh etc.
See table B2 http://www.tldp.org/LDP/abs/html/refcards.html#AEN22078 and note this changes inside (( )) arithmetic brackets.

The issue here is [ ] vs [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
Code:
if [[ 4 < 5 ]]; then echo TRUE; fi
TRUE
 
Old 07-21-2011, 04:16 AM   #25
bytecode
LQ Newbie
 
Registered: Oct 2009
Location: Cornwall/Devon
Distribution: *Bunty/Debian/Fedora/Gentoo/Mandriva
Posts: 3

Rep: Reputation: 1
Ah, yes - I wasn't aware of the double [[ ]] context with regards to escaping.
Now, If only we can get around the string data type issue whereby this unexpectedly returns TRUE:

Code:
$ if [[ 40.0 < 5.0 ]]; then echo TRUE; else echo FALSE; fi
TRUE
Of course the fun with (( )) is again that we often want to compare floats, if comparing with say, CPU or MEM% use of a process etc..
I suppose we could always go for integer arithmetic; multiply by 100 and discard the decimal fractional component?

Any further tips chrism01?
=)

Quote:
Originally Posted by chrism01 View Post
Strictly speaking, '< > = == ... ' are string operators, '-eq -ne -lt ...' are numeric operators (in a test statement) in bash, ksh etc.
See table B2 http://www.tldp.org/LDP/abs/html/refcards.html#AEN22078 and note this changes inside (( )) arithmetic brackets.

The issue here is [ ] vs [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
Code:
if [[ 4 < 5 ]]; then echo TRUE; fi
TRUE
 
Old 11-26-2011, 03:19 AM   #26
frater
Member
 
Registered: Jul 2008
Posts: 121

Rep: Reputation: 23
I don't know what's worse... someone else telling you that you are wrong or yourself..

But it now seems that 'expr' isn't suitable to compare "floating numbers"

Code:
# x=3.3
# y=3.2
# expr $x \> $y >/dev/null && echo "$x > $y"
3.3 > 3.2
# x=4
# y=3.2
# expr $x \> $y >/dev/null && echo "$x > $y"
4 > 3.2
# x=10
# y=3
# expr $x \> $y >/dev/null && echo "$x > $y"
10 > 3
# x=10
# y=3.2
# expr $x \> $y >/dev/null && echo "$x > $y"
#
Although it doesn't give an error, it doesn't work in the last case....
I guess you can do it better with awk....

Last edited by frater; 11-26-2011 at 03:27 AM.
 
Old 11-27-2011, 02:24 PM   #27
bytecode
LQ Newbie
 
Registered: Oct 2009
Location: Cornwall/Devon
Distribution: *Bunty/Debian/Fedora/Gentoo/Mandriva
Posts: 3

Rep: Reputation: 1
Quote:
Originally Posted by frater View Post
I don't know what's worse... someone else telling you that you are wrong or yourself..

But it now seems that 'expr' isn't suitable to compare "floating numbers"

Code:
# x=3.3
# y=3.2
# expr $x \> $y >/dev/null && echo "$x > $y"
3.3 > 3.2
# x=4
# y=3.2
# expr $x \> $y >/dev/null && echo "$x > $y"
4 > 3.2
# x=10
# y=3
# expr $x \> $y >/dev/null && echo "$x > $y"
10 > 3
# x=10
# y=3.2
# expr $x \> $y >/dev/null && echo "$x > $y"
#
Although it doesn't give an error, it doesn't work in the last case....
I guess you can do it better with awk....
Or we're back to "bc" again...
 
Old 11-28-2011, 07:23 PM   #28
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
A few weeks ago I wrote this one to compare floats in bash, it handles negatives too:

Code:
#!/bin/bash
shopt -s extglob
fcomp() {
    local oldIFS="$IFS" op=$2 x y digitx digity
    IFS='.' x=( ${1##+([0]|[-]|[+])}) y=( ${3##+([0]|[-]|[+])}) IFS="$oldIFS"
    while [[ "${x[1]}${y[1]}" =~ [^0] ]]; do
        digitx=${x[1]:0:1} digity=${y[1]:0:1}
        (( x[0] = x[0] * 10 + ${digitx:-0} , y[0] = y[0] * 10 + ${digity:-0} ))
        x[1]=${x[1]:1} y[1]=${y[1]:1} 
    done
    [[ ${1:0:1} == '-' ]] && (( x[0] *= -1 ))
    [[ ${3:0:1} == '-' ]] && (( y[0] *= -1 ))
    (( ${x:-0} $op ${y:-0} ))
}

for op in '==' '!=' '>' '<' '<=' '>='; do
    fcomp $1 $op $2 && echo "$1 $op $2"
done
Edit:
sorry I forgot to explain how it works, it works essentially by shifting the point until both numbers are ints (ie, iterate: int_part * 10 + shifted decimal digit).

Its a bit verbose to take into account some oddities, such as numbers with leftmost zeroes at the int part or rightmost zeroes at the decimal part, and signed numbers. If you don't use signs you can dismiss the two lines before the last in fcomp():
Code:
    [[ ${1:0:1} == '-' ]] && (( x[0] *= -1 ))
    [[ ${3:0:1} == '-' ]] && (( y[0] *= -1 ))
And shorten the patterns at x and y array creation to:
Code:
    IFS='.' x=( ${1##+([0])} ) y=( ${3##+([0])} ) IFS="$oldIFS"
so it just wipes the leftmost zeroes.

Last edited by Juako; 11-29-2011 at 07:31 AM.
 
1 members found this post helpful.
Old 09-10-2012, 08:43 AM   #29
hayko
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Rep: Reputation: Disabled
Hi
Please help me.
I have file which contains
60
59
52
45
43
40
70
69
62
which bash script can give me "pass on every line and every time remembering previous number give me the first mutch where next number greater than previous, in this example 70"
Please help thank you.
 
Old 09-10-2012, 11:29 AM   #30
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This should get you started:
Code:
read xOld < LQhayko.dat; 
while read xNew; do 
    echo $xNew; 
    if [ $xNew -gt $xOld ]; then 
        echo "Found rising edge: $xNew > $xOld"; 
        break;
    else 
        xOld=$xNew; 
    fi;  
done < LQhayko.dat
--- rod.

PS. This should have been posted as a new thread. Necroposting and thread hijacking are considered poor form here.

Last edited by theNbomr; 09-10-2012 at 11:34 AM.
 
  


Reply



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
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
How to compare records in two tables in seperate My Sql database using shell script sumitarun Programming 5 04-14-2005 09:45 AM
compare date uusing shell programming please... izza_azhar Programming 7 01-14-2005 07:24 AM
Need text pattern compare script kscott121 Linux - Software 4 05-10-2004 01:13 PM
Help with a Directory Compare Script bullfrog Linux - General 1 02-04-2003 08:05 AM

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

All times are GMT -5. The time now is 02:05 AM.

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
Open Source Consulting | Domain Registration