The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
EOF checking the below ramkrix High Level Programming 10 03-11-2008 01:43 AM
checking uid filthymonk Shell Programming and Scripting 7 07-19-2007 10:40 PM
Checking cp progress MarGur UNIX for Dummies Questions & Answers 0 05-15-2007 04:13 PM
Checking for PXE maestro@altiris SUN Solaris 5 05-25-2004 12:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-07-2008
philplasma's Avatar
philplasma philplasma is offline
Registered User
  
 

Join Date: May 2007
Location: Quebec
Posts: 18
Question checking for non-zero value

I have a process that spits out a file called sqlplus.out, here is what the result looks like:
Quote:
$ cat sqlplus.out

MAX_ID-LAST_ID
--------------
0
Currently the value you see is zero, what I need to do is perform an action if that value is non-zero, so how do I check that value in an if statement? If it helps at this moment in development the line with the value will always be on line 4.

Thanks for the help!
  #2 (permalink)  
Old 01-07-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
if then for non-zero

You can do it simpler, but I am assuming that you might have several things to do when a non-zero is found.
This is looking for the last one line of the file; knowing that the value will be on that line.


x=$(tail -1 sqlplus.out)

echo $x
if [ $x -ne 0 ]
then
echo "You have found a non-zero value"
fi
  #3 (permalink)  
Old 01-07-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is online now Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 921
Quote:
Originally Posted by joeyg View Post
You can do it simpler, but I am assuming that you might have several things to do when a non-zero is found.
This is looking for the last one line of the file; knowing that the value will be on that line.


x=$(tail -1 sqlplus.out)

echo $x
if [ $x -ne 0 ]
then
echo "You have found a non-zero value"
fi
Won't run in bourne, for a bourne-safe syntax, use back ticks:
Code:
if [ `tail -1 sqlplus.out` -ne 0 ] ; then echo "You have found a non-zero value" ; fi
  #4 (permalink)  
Old 01-08-2008
philplasma's Avatar
philplasma philplasma is offline
Registered User
  
 

Join Date: May 2007
Location: Quebec
Posts: 18
Thumbs down

without the back ticks it didn't work, so thanks go to the Smiling Dragon for helping out and thanks joeyg for the attempt. However, it still doesn't work, I get the following as a result:
Code:
" near 0wn operator in arithmetic expression "0
There is no quotation mark present in the sqlplus.out file, though I decided to vi the file and found that there is a ^M character at the end of each line. I should let you know that this is a unix emulator on a windows server. Another thing to mention is that the result in the sqlplus.out file does not start at the first character, sqlplus right justifies the value with the field header - for whatever reason the unix.com forum interface put it to the left. Thanks again for the assistance!
  #5 (permalink)  
Old 01-08-2008
jadionne jadionne is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 39
just a side note what DB are using to get the data out of?
I know if you use certain operaters in DB2 you can cut out all the unneeded stuff like column headers I.E.

Code:
db2 -x "select * from your.table"
If you can get your DB to cut out all the crap then all you will have to worrie about is comparing the values and you can throw out the "tail" command.

Just a thought.

Jim
  #6 (permalink)  
Old 01-08-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is online now Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 921
Quote:
Originally Posted by philplasma View Post
without the back ticks it didn't work, so thanks go to the Smiling Dragon for helping out and thanks joeyg for the attempt. However, it still doesn't work, I get the following as a result:
Code:
" near 0wn operator in arithmetic expression "0
There is no quotation mark present in the sqlplus.out file, though I decided to vi the file and found that there is a ^M character at the end of each line. I should let you know that this is a unix emulator on a windows server. Another thing to mention is that the result in the sqlplus.out file does not start at the first character, sqlplus right justifies the value with the field header - for whatever reason the unix.com forum interface put it to the left. Thanks again for the assistance!
Ah, in that case, we can make the search for 0 on the last line a little more robust, the following will trigger if you get a non-zero exit code but will ignore any non-digit characters on the line. You could add a second grep for a 0 afterwards to confirm it's given an exit code at all but I've left that off in the example below:
Code:
if `tail -1 sqlplus.out | egrep '[1-9]'` ; then ...
  #7 (permalink)  
Old 01-08-2008
philplasma's Avatar
philplasma philplasma is offline
Registered User
  
 

Join Date: May 2007
Location: Quebec
Posts: 18
Thumbs up

Thanks!

While I am sure your solution works perfectly, I was able to work around the problem by changing my SQL that produces the difference between last id and max id and output 'true' if there is a difference. Now I just need to grep on true from the sqlplus.out file and that satisfies the condition.

As always unix.com delivers, in my case not a final solution, but helpful hints to get me there.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0