LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-25-2006, 05:24 PM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
bash string comparison


My script takes user input and compare it with some values. It works fine when it compares against 0.00 or 10 or 100. But if the user input is something like "-0.009" it gives error - "integer expression expected in line 7 and 9" where it ought to print the number like -

your number is -0.009.

Here's the code -

Code:
if [ "$num" = 0.00 ] || [ "$num" -le 10 ]; then      # this is line 7
  echo "your number is $num"
elif [ "$num" -ge "100" ]; then                      # this is line 9
  echo "your number is greater than 100"
else
  echo "your number is $num"

Last edited by noir911; 01-25-2006 at 05:36 PM.
 
Old 01-25-2006, 06:37 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
For floating point operations, you need to invoke bc. See the Advanced Bash-Scripting Guide for examples.

Alternatively, you can use another scripting language.
 
  


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
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
Perl String comparison Xris718 Linux - General 5 04-01-2005 01:59 PM
String Input & Array Comparison Problem azucarmom Programming 2 03-13-2005 07:23 AM
perl string comparison problem AM1SHFURN1TURE Programming 3 03-06-2005 10:29 AM
Image Comparison With Bash Script smcallis Programming 4 06-30-2004 12:53 PM

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

All times are GMT -5. The time now is 10: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