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 |
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 ]] -- Ghodmode |
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 - Code:
% ./s2 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) |
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") |
Hi.
If you are using bash, there are restrictions on arithmetic: Quote:
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) |
same as makyo's idea, i thought of making a script
Code:
#!/bin/bash i really think this was just a waste of time but i thought i'll just finish and post it. |
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 |
that won't work all the time.. check out
Code:
[[ 2.0 < 1234.0 ]] && echo a |
edit:
Code:
#!/bin/bash |
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 |
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 Code:
$php my_script.php some_value Code:
if ($argc > 1) print $argv[1] . "\n"; Code:
$date = `date`; Regards, SIMP Fedora Development |
Quote:
|
All times are GMT -5. The time now is 04:33 AM. |