LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 03-14-2012, 03:19 PM   #1
linuxandtsm
Member
 
Registered: May 2011
Posts: 191

Rep: Reputation: Disabled
bash script error : integer expression expected


Hi all,

getting an error in bash script.

./test.sh: line 6: [: 0.9: integer expression expected

and the code at line 6 is

Code:
if [ "$slope"  -gt  "80.0" ]; then
Tried
Code:
if [ $slope  -gt  80.0 ]; then

and also

if [ "$slope"  -gt  80 ]; then
but no luck
i am sure there is something wrong with my comparison in above code but not sure what exactly. Any help ?
Thanks in advance!

Last edited by linuxandtsm; 03-14-2012 at 03:21 PM.
 
Old 03-14-2012, 03:35 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,689

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
@ Reply

Hi linuxandtsm,

Appears to be the way you are mentioning then number. You are putting it as 80.0 which is not integer instead it is a floating point. In the last attempt when you used 80 it appears that you have given extra space in the code.

I created a sample script and it works fine. See below:

Code:
#!/bin/bash
echo "Enter a number"
read slope
        if [ "$slope" -gt 80 ]
        then
                echo "Number entered is greated than 80"
        else
                echo "Number entered is less than 80"
        fi
 
Old 03-14-2012, 04:03 PM   #3
linuxandtsm
Member
 
Registered: May 2011
Posts: 191

Original Poster
Rep: Reputation: Disabled
Hi T3RM1NVT0R,
Thank you for quick reply.

I tried below but endup with same error (not sure if i understood your comment on extra space)
Code:
if [ "$slope" -gt 80 ]; then

############ got below error
./test.sh: line 6: [: 0.9: integer expression expected

Last edited by linuxandtsm; 03-14-2012 at 04:06 PM.
 
Old 03-14-2012, 04:10 PM   #4
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,689

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
@ Reply

Is it possible for you to share the script. It is working for me so not sure whats wrong with your script.
 
Old 03-15-2012, 01:04 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993
That means that $slope contains a float (0.9).

1. use
Code:
set -xv
at the top of your prog to debug it

2. default shell operators like -eq, -gt etc only operate on integers anyway; use eg bc http://linux.die.net/man/1/bc

http://rute.2038bug.com/index.html.gz
 
1 members found this post helpful.
Old 03-15-2012, 04:38 AM   #6
dominic-dl
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
This uses awk to do the floating-point comparison:

Code:
TEST=80.2
echo $TEST|awk '{if ($1>80) {print "bigger";} else print "smaller or same";}
 
Old 03-15-2012, 02:15 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
See this link for a detailed description of bash's built-in integer arithmetic:

arithmetic expressions

And see here for floating point operations:

http://mywiki.wooledge.org/BashFAQ/022


Incidentally, when doing integer comparisons in bash or ksh, it's generally recommended to use the ((..)) arithmetic field, rather than the default test brackets.

Code:
if (( slope > 80 )); then
Assuming again that "slope" contains an integer value of course.

For string and filename comparisons, it's also usually recommended to use the more advanced [[ keyword over the old [ command.

http://mywiki.wooledge.org/BashFAQ/031
 
1 members found this post helpful.
Old 01-07-2014, 06:01 AM   #8
LennyNero
LQ Newbie
 
Registered: Jul 2005
Location: south of the river
Distribution: Slackware 13.37 main & others
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by David the H. View Post
Thank you for that link looks like there is a load of good reading there.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 05:59 PM
C compiling error: expected expression before { token CoderMan Programming 3 03-31-2009 04:31 PM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM
bash shell script expression CowboyJ Programming 2 04-24-2005 11:33 PM
Error upgrading kernel, /sbin/mkinitrdi integer expression expected amp2000 Linux - General 2 12-05-2003 05:38 AM


All times are GMT -5. The time now is 11:47 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration