Hi I'm a newbie in unix scripting and I'm trying to do a function that do the next:
Run this command: opcdeploy -cmd "utility -xs" -node node.com and store the result in a file.
Get the last ten lines from the stored file and do a search for the number 100, store that search in a variable.
If the result of that search is greater or equal than 100 then echo "hello"; else echo "bye".
Here is what I have done:
function utility {
opcdeploy -cmd "utility -xs" -node $SERVER | tail -10 > /home/oscar/Desktop/tail.util
let Z=100
let Y=`grep $Z /home/oscar/Desktop/tail.util`
if [ $Y -ge $Z ];then
echo "hello"
else
echo "bye"
fi
}
please can someone help me to improve this, what I have done wrong?
Grep for "100" will not find most values greater than 100 (eg 372, 2000, etc).
What does the output look like, and what value are you wanting to compare with 100?
The Global file is now 76.3% full with room for 15.0 more full days
The Application file is now 91.6% full with room for 8.7 more full days
The Process file is now 98.4% full with room for 0.7 more full days
The Device file is now 80.1% full with room for 3.3 more full days
The Transaction file is now 100.7% full with room for 0.0 more full days
So I want to get the % number and compare it, for example:
Get this number 76.3 and put it into a variable to compare(-ge) it with the number 100; then
echo bla bla bla
With "-q", grep doesn't print anything. From its man:
So, thanks to you, we both know now that, for portability reason, it's better to use the redirection instead of -q or -s. There's always to learn. Thanks!
Thanks for the info.
I tried without the -q too, take a look:
---------- Post updated at 08:49 PM ---------- Previous update was at 07:10 PM ----------
Finally I found a solution with sed command:
root@oscar-VirtualBox:/home/oscar# sed -r 's/^[^0-9]*([0-9]+.[0-9]).*/\1/' /home/oscar/Desktop/tail.util
76.3
root@oscar-VirtualBox:/home/oscar#
but now if I try t set the output(76.3) into a variable I got the command insted of the number 76.3 when do an "echo":
root@oscar-VirtualBox:/home/oscar# a="sed -r 's/^[^0-9]*([0-9]+.[0-9]).*/\1/' /home/oscar/oscar.txt"
root@oscar-VirtualBox:/home/oscar# echo $a
sed -r 's/^[^0-9]*([0-9]+.[0-9]).*/\1/' /home/oscar/oscar.txt
root@oscar-VirtualBox:/home/oscar#
$ cat tail.util
The Device file is now 80.1% full with room for 3.3 more full days
$ egrep -q "[1-9][0-9]{2,}\.*[0-9]*\%" tail.util && echo hello || echo bye
bye
$ cat tail.util
The Device file is now 180.1% full with room for 3.3 more full days
$ egrep -q "[1-9][0-9]{2,}\.*[0-9]*\%" tail.util && echo hello || echo bye
hello
if you want to do more than echo something and if statement might be best:
if egrep -q "[1-9][0-9]{2,}\.*[0-9]*\%" tail.util
then
echo hello
else
echo bye
fi
or if you want to act on each line > 100%
egrep -o "[1-9][0-9]{2,}\.*[0-9]*\%" tail.util | while read PCT
do
echo "found value $PCT greater than 100%"
done