UNIX shell script parse a line

toooki
toooki used Ask the Experts™
on
I have a file in my UNIX server whose content is like this:
#cat myfile.txt
        25
#
OR:
#cat myfile.txt
        0
#

I need to parse the content to find out if the number is > 0. There are leading and trailing spaces in the line myfile.txt .
How could I compare the content... Any suggestion could be very helpful..Below is my code and pseudo code... Thnak you!

##Content of the .sh file:
#!/bin/sh
tmp=/tmp/mail-body1-`date +%F`;
touch $tmp && chmod 600 $tmp;
/bin/cat /usr/mydir/myfile.txt >> $tmp

#Now this is what I want to do:

IF $tmp > 0

THEN
###
/usr/sbin/sendmail -t < $msg;
##

ELSE
##
do nothing
###
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013
Commented:
value=$(cat $tmp) # or value=$(cat myfile.txt)

if [[ $value -gt 0 ]]; then
 # sendmail
fi

Author

Commented:
Thank you.

I tried:

/bin/cat /usr/mydir/myfile.txt >> $tmp
value=$(cat $tmp)
if [[ $value -lt 0 ]]; then
/usr/sbin/sendmail -t < $SUBJECT
fi

Also tried:
/bin/cat /usr/mydir/myfile.txt >> $tmp
value=$(cat $tmp)
if [[ $value -gt 0 ]]; then
/usr/sbin/sendmail -t < $SUBJECT
fi

It does not go to the sendmail part.
the code alone:
/usr/sbin/sendmail -t < $SUBJECT
The above works.

Thanks.
Most Valuable Expert 2013
Top Expert 2013
Commented:
Is the file whose name ("/tmp/mail-body1-`date +%F`") is stored in $tmp really empty before appending myfile.txt to it?

This can't be seen from your script. There's just a "touch" but the file could have existed before.
To really start with a fresh file use ">" instead of ">>"

Anyway, for my suggestion to work $tmp may contain just one line which holds the number.
Spaces or TABs don't matter.

Another possibility: In the examples you posted in your last comment you don't test for "0".
Could this be the culprit?

wmp
Commented:
What i will do is the following:

value=`cat /usr/mydir/myfile.tx`

if [ "$value" -gt 0 ]; then
/usr/mydir/myfile.tx
fi

Regards
Top Expert 2007
Commented:
It would be useful to explain the whole logic of what you want to do, as it isn't clear from your code snippet.

Perhaps you want

#!/bin/sh
tmp=/tmp/mail-body1-`date +%F`;
touch $tmp && chmod 600 $tmp;
/bin/cat /usr/mydir/myfile.txt >> $tmp

if [ `cat /usr/mydir/myfile.txt` -eq 0 ]
then
      .....
fi

Open in new window

Gabriel OrozcoSolution Architect
Commented:
I believe you may better have this simply:
value=$(awk '{print $1}' /usr/mydir/myfile.txt)
if [ $value -ne 0 ]; then
   /usr/sbin/sendmail -t < $SUBJECT
fi

Open in new window


note you were checking for $value being less than zero, which is not happening ever. it's simpler to check if the value IS NOT zero :-)

Author

Commented:
Worked thank you!
Gabriel OrozcoSolution Architect

Commented:
Glad it worked

would you mind to award points and close the question?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial