LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
  Search this Thread
Old 07-21-2010, 02:20 AM   #1
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Rep: Reputation: 30
bash command


I use the below script to check if the output of $file1 is great than 10.01 , then do xxxx , but after run the script , it pops "integer expression expected" , can advise what should i change the script ? thx

if [ "$file1" -gt 10.01 ]

then
xxxx
fi
 
Old 07-21-2010, 02:34 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
You'd want to check bc if you wish to do non-integer checks. alternatively multiply both sides by 100 to make them integers.
 
Old 07-21-2010, 04:11 AM   #3
Filip_Kv
LQ Newbie
 
Registered: Mar 2010
Location: Thessaloniki, Greece
Distribution: Debian, Ubuntu, CentOS, Fedora
Posts: 11

Rep: Reputation: 2
Wink Re: bash command

Goodmorning,
The problem with this script is that it uses floating point numbers,
which by default the bash shell cannot handle. In order to use it right
you will have to make the comparison with an external program (inside
your script) and pass the result of that comparison to the conditional
if statement.

In order to do that you will have to use bc, a programming
language calculator that can be used for such purposes.

What I do in the script is that I first read the input of the file
to a variable (a step that can be ommitted but is put here for clarity)
and then I compare the floating point of that variable
to whatever other number I want. The result of the comparison is 1 if
it is true or 0 if not. Then, I check the result if it is 1 or 0 and I
proceed accordingly.

I also have a text file, named file1, containing the number 10.02, or 9.04 for testing purposes. The same name is used for the variable, but you can use any name.

Here is the code that you should use:

Code:
#!/bin/bash

file1=`cat "$1"`
result=`echo "scale=2; $file1 > 10.02" | bc`
if [ $result -eq 1 ]
then
echo "greater than 10.02"
else
echo "not greater than 10.02"
fi
The scale command is used to tell bc the number of
decimals to use after the point for the float. You can change that to
3,4,5, etc.

Gor more information on bc use man bc.

I hope that I have helped.

-Fk

Last edited by Filip_Kv; 07-21-2010 at 04:15 AM.
 
  


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
[SOLVED] Is there any command to delete bash command history? cola Linux - Newbie 4 06-01-2010 08:32 AM
Bash history delete command from bash itself ashishag Linux - Software 6 05-02-2010 04:39 AM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 12:21 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Automatically append another piped command to issued command in bash amateen Programming 1 05-07-2009 07:36 AM


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