LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 03-19-2008, 12:17 PM   #1
irfanb146
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Rep: Reputation: 0
Shell script logical opeartors


Following the condition of sending mail
whenever any condition true the mail will go with warning, other wise mail will go with info.

Problem here is that whenever i put condition $f1<100000. the condition is not working and it is working when the digit is less than 100,000.


I want to find out why the condition is not working if digit is greater than 100,000

if [[ $d1>0.80 || $p1>70 || $f1<50000 || $g1<100 ]]
then
echo $f1
echo "Free disk space is $g1 GB,Free memory is $f1 KB,Processes are $p1,$b1"|mail -s "WARNING: in db CPU% is $c1" abc@xyz.com
else
echo $f1
echo "Free disk space is $g1 GB,Free memory is $f1 KB,Processes are $p1,$b1"|mail -s "INFO: in db CPU% is $c1" abc@xyz.com
fi
 
Old 03-20-2008, 02:29 AM   #2
Ghodmode
LQ Newbie
 
Registered: Feb 2007
Location: New Zealand
Distribution: Kubuntu
Posts: 11

Rep: Reputation: 0
Your operators are wrong. In shell scripting the greater-than and less-than signs are used for output redirection. See if you're inadvertently creating files named "0.80", "70", and named after the values of the variables $f1 and $g1.

To test for greater-than and less-than, use -gt and -lt.

ref: man test

Code:
if [[ $d1 -gt 0.80 || $p1 -gt 70 || $f1 -lt 50000 || $g1 -lt 100 ]]
then
	echo $f1
	echo "Free disk space is $g1 GB,Free memory is $f1 KB,Processes are $p1,$b1"|mail -s "WARNING: in db CPU% is $c1" abc@xyz.com
else
	echo $f1
	echo "Free disk space is $g1 GB,Free memory is $f1 KB,Processes are $p1,$b1"|mail -s "INFO: in db CPU% is $c1" abc@xyz.com
fi
--
-- Ghodmode
 
Old 03-20-2008, 06:04 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

It may be that the items in the comparison are not what you thought they were.

Here's an example that uses the relational operator (permissible within [[ ... ]]) and shows how it can fail:
Code:
#!/bin/bash -

# @(#) s2       Demonstrate evaluation in bash.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)

one=1
big=100000

echo
echo " Files currently extant:"
ls

echo
if [[ $one < $big ]]
then
  echo " one, $one, tests less than big, $big"
else
  echo " unexpected failure in comparison."
fi

if [[ $one<$big ]]
then
  echo " one, $one, tests less than big, $big"
else
  echo " unexpected failure in comparison."
fi

big="junk"
if [[ $one<$big ]]
then
  echo " one, $one, tests less than big, $big"
else
  echo " unexpected failure in comparison."
fi

big="/junk"
if [[ $one<$big ]]
then
  echo " one, $one, tests less than big, $big"
else
  echo " one, $one, tests apparently greater than big, $big"
fi

echo
echo " Files currently extant:"
ls

exit 0
Producing:
Code:
% ./s2

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Files currently extant:
readme.txt  s1  s2

 one, 1, tests less than big, 100000
 one, 1, tests less than big, 100000
 one, 1, tests less than big, junk
 one, 1, tests apparently greater than big, /junk

 Files currently extant:
readme.txt  s1  s2
EDIT 1: However, these are string tests, which is why there are no syntax errors posted. Confusingly, there is an alternate syntax for arithmetic tests that uses "<", but it is used with ((...)) -- see the 2-column table at http://www.tldp.org/LDP/abs/html/refcards.html#AEN20389 for details (left column numeric, right column string).

I suggest you post the smallest example of failed code along as well as the results. Please use code, /code tags around the code sections so that we can more easily read them ... cheers, makyo

(edit: clarify type of tests, add link)

Last edited by makyo; 03-28-2008 at 06:44 PM.
 
Old 03-25-2008, 11:12 AM   #4
irfanb146
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Yes everything is working fine after i convert < to -lt and > to -gt

But following problem is occuring when i am using it with decimal number

0.380000 -lt 80

++ [[ 0.380000 -lt 80 ]]
./dada1.sh: line 10: [[: 0.380000: syntax error in expression (error token is ".380000")
 
Old 03-25-2008, 12:16 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

If you are using bash, there are restrictions on arithmetic:
Quote:
ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain
circumstances (see the let builtin command and Arithmetic Expansion).
Evaluation is done in fixed-width integers with no check for overflow,
though division by 0 is trapped and flagged as an error.

-- excerpt from man bash
The Korn shell ksh and zsh can perform floating point arithmetic. Otherwise, you could do some scaling of the numbers to make them integers and then compare the new values.

You might be able to arrange the use of bc, which can also do floating point arithmetic, but you would need to pass the result of the comparison back as an exit status or some value on standard output. I haven't tried that, however. If I get a chance, I'll investigate that.

Best wishes ... cheers, makyo

(edit: add detail for bc)

Last edited by makyo; 03-25-2008 at 09:10 PM.
 
Old 03-26-2008, 05:31 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
same as makyo's idea, i thought of making a script
Code:
#!/bin/bash

errorexit() { echo "$1" >&2; exit 1; }

testf_decs() {
	local -i a b ac bc
	a=${1} b=${2} ac=${#a} bc=${#b}
	if [[ ac -ne bc ]]; then
		[[ ac -lt bc ]] && (( a = a*(10**(bc-ac)) )) || (( b = b*(10**(ac-bc)) ))
	fi
	eval "[[ ${a} ${2} ${b} ]]"
	return
}

testf() {
	[ $# -ne 3 ] && errorexit "invalid number of parameters"

	[[ $1 =~ ^[0-9]+\.{0,1}[0-9]*$ ]] || errorexit "first parameter is probably not a number"
	[[ $3 =~ ^[0-9]+\.{0,1}[0-9]*$ ]] || errorexit "third parameter is probably not a number"

	local -i a b x y

	if [[ ${1} =~ \. ]]; then
		a=${1%.*}
		x=${1#*.}
	else
		a=${1}
		x=0
	fi

	if [[ ${3} =~ \. ]]; then
		b=${3%.*}
		y=${3#*.}
	else
		b=${3}
		y=0
	fi

	case ${2} in
		-eq|-ne)
			eval "[[ ${a} ${2} ${b} ]]" && testf_decs ${x} ${2} ${y}
			return
			;;
		-ge|-le|-gt|-lt)
			if [[ ${a} -eq ${b} ]]; then
				testf_decs ${x} ${2} ${y}
				return
			else
				eval "[[ ${a} ${2} ${b} ]]"
				return
			fi
			;;
		*)
			errorexit "invalid operator ${2}"
			;;
	esac
}

testf $*
exit $?
the script got so long i never thought it would be this complicated.. splitting the numbers to arrays should make easier output but i hate to use external programs like grep or sed in this kind of script. it will lose its essence.. if we use external progs, we better just use those like bc instead as also just what makyo has said.

i really think this was just a waste of time but i thought i'll just finish and post it.

Last edited by konsolebox; 03-26-2008 at 05:36 AM.
 
Old 03-26-2008, 05:44 AM   #7
irfanb146
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
But < is doing fine with floating number. Is it better to use < instead of -lt then whenever there is a floating number we use < instead of -lt.

Is it correct
 
Old 03-26-2008, 09:52 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
that won't work all the time.. check out

Code:
[[ 2.0 < 1234.0 ]] && echo a
 
Old 03-26-2008, 10:53 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
edit:
Code:
#!/bin/bash

testfloat() {
	errorexit() { echo "$1" >&2; exit 1; }

	[ $# -ne 3 ] && errorexit "invalid number of parameters"

	[[ $1 =~ ^[0-9]+\.{0,1}[0-9]*$ ]] || errorexit "first parameter is probably not a number"
	[[ $3 =~ ^[0-9]+\.{0,1}[0-9]*$ ]] || errorexit "third parameter is probably not a number"

	case ${2} in
		-eq|-ne|-ge|-le|-gt|-lt)
			;;
		*)
			errorexit "invalid operator ${2}"
			;;
	esac

	local -i a b x y

	if [[ ${1} =~ \. ]]; then
		a=${1%.*}
		x=1${1#*.}
	else
		a=${1}
		x=1
	fi

	if [[ ${3} =~ \. ]]; then
		b=${3%.*}
		y=1${3#*.}
	else
		b=${3}
		y=1
	fi

	if [[ ${a} -eq ${b} ]]; then
		local -i xc yc
		xc=${#x} yc=${#y}
		if [[ xc -ne yc ]]; then
			[[ xc -lt yc ]] && (( x = x*(10**(yc-xc)) )) || (( y = y*(10**(xc-yc)) ))
		fi
		eval "[[ ${x} ${2} ${y} ]]"
		return
	else
		eval "[[ ${a} ${2} ${b} ]]"
		return
	fi
}

testfloat $*
exit $?

Last edited by konsolebox; 03-29-2008 at 10:14 PM. Reason: decimal bug.. added 1s as prefix
 
Old 03-27-2008, 03:06 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
I think you can do floating point if you use true arithmetic tests; see bottom of the page:http://www.tldp.org/LDP/abs/html/testconstructs.html

For the full list of which operators to use with which type of value, see: http://www.tldp.org/LDP/abs/html/comparison-ops.html
 
Old 03-27-2008, 01:03 PM   #11
simplicissimus
Registered User
 
Registered: Mar 2008
Posts: 104

Rep: Reputation: 15
PHP for commandline scripting

If you are familiar with C-style coding and have PHP installed, than it might make sense to use PHP for scripting.

You can run it like
Code:
$php my_script.php
pass parameters on the commandline
Code:
$php my_script.php some_value
and read these params within the code like
Code:
if ($argc > 1) print $argv[1] . "\n";
PHP has no problems with floating-point numbers and you can mix it with other UNIX utilities by using backticks or a system call
Code:
$date = `date`;
$filename = "myfile-".$date";
system ("echo 'this' > $filename");
Hope this helps,
Regards,
SIMP

Fedora Development

Last edited by simplicissimus; 04-02-2008 at 06:01 AM.
 
Old 03-27-2008, 10:31 PM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by chrism01 View Post
I think you can do floating point if you use true arithmetic tests; see bottom of the page:http://www.tldp.org/LDP/abs/html/testconstructs.html

For the full list of which operators to use with which type of value, see: http://www.tldp.org/LDP/abs/html/comparison-ops.html
i think those are only about integers
 
  


Reply

Tags
shell


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
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 07:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 03:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration