Help with function

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?

thanks!

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?

I want to search for a number on the output of tail and if that number is greater or equal to 100 then do...

Please post sample output of the last 10 lines from the opcdeploy ... command, highlighting where one might find a number with value 100 .

Thanks, the opcdeploy output is:

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

else
echo bla bla bla

fi

Hi!
Why would you need a comparison?
If you can read 100, you have at least 100. So if you can read 100, you're done.

Hint:

 ( (egrep -q  "100\.*[0-9]*\%" /home/oscar/Desktop/tail.util ) && echo hello ) || echo bye

If you run your script frequently enough, you'll catch greater values in time (which I though was your goal).

However:

 ( (egrep -q  "[1-9][0-9]{2,}\.*[0-9]*\%" /home/oscar/Desktop/tail.util ) && echo hello ) || echo bye

will match every percent number greater or equal than 100%.

Thank you Lem I tried this:

egrep -q  "[1-9][0-9]{2,}\.*[0-9]*\%" /home/oscar/Desktop/tail.util

but it didnt display any output, insted of that I tried this:

sed "s/[^0-9]//g;s/^$/-1/;" /home/oscar/Desktop/tail.util

and got this output for example for the first ovcodautil line:

"The Global      file is now  76.3% full with room for  15.0 more full days"

the output was 763150, but I need only the first three as they are on the line(76.3), any idea?

Of course: that's the meaning of "-q" option.

If you want to see the output, try with:

egrep "[1-9][0-9]{2,}\.*[0-9]*\%" /home/oscar/Desktop/tail.util

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! :slight_smile:

In the examples, the subshells can be left out:

egrep -q ... && echo hello || echo bye

or

if egrep -q ...    ; then

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# 

what I'm doing wrong?

Perhaps this will help:

$ 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

To do that you would need command substitution:

a=$( sed -r 's/^[^0-9]*([0-9]+.[0-9]).*/\1/' /home/oscar/Desktop/tail.util )
echo "$a"