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
cannot get the logic dineshr85 Shell Programming and Scripting 3 10-11-2007 07:34 AM
expand logic for > and < pbsrinivas Shell Programming and Scripting 0 08-10-2007 09:59 AM
Strange Logic ganesh123 Shell Programming and Scripting 5 03-20-2007 05:08 PM
Need help in genrating the logic amitjha Shell Programming and Scripting 6 11-08-2006 06:45 AM
what the logic ramneek IP Networking 2 09-05-2005 07:42 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 11-21-2007
jadionne jadionne is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 39
If Then Else Logic

I am obviously new to Unix Script writing, and I think I am trying to do this in SQL when im not in a SQL environment. What I need to do is the following

Code:
EXTRACT_ROW_COUNT=`grep Number $REFERENCE_LOG"extract_concern.log" | sed '/Number of rows exported:/s/.*:[^0-9]*\([0-9][0-9]*\)$/\1/'`
LOADED_ROW_COUNT=`db2 -x "select count(*) from CENREF.REF_CONCERN_LU_TBL_B"`

IF $EXTRACT_ROW_COUNT <> 0 and $EXTRACT_ROW_COUNT = $LOADED_ROW_COUNT 
THEN nohup $REFERENCE_SCRIPT"renme_ref_concern.sh" & 
ELSE echo "LOAD INCOMPLETE" > $REFERENCE_LOG"CONCERN_LOAD_FAILURE.LOG" 
end IF

I have been studing the IF man pages but they dont seem to do what I need. Im probably overlooking something, can you point me in the right direction?
  #2 (permalink)  
Old 11-22-2007
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
It does appear that you're using SQL syntax in the shell

What shell do you use? What platform? (uname -a; echo $SHELL)

The syntax of `if` varies by shell and version, somewhat - here's an example that should work in bash, ksh, or POSIX shell (make sure to test it first!):
Code:
if [ $EXTRACT_ROW_COUNT > 0 -a $EXTRACT_ROW_COUNT = $LOADED_ROW_COUNT ]; then
     nohup $REFERENCE_SCRIPT"renme_ref_concern.sh" &
   else
     echo "LOAD INCOMPLETE" > $REFERENCE_LOG"CONCERN_LOAD_FAILURE.LOG"
fi
There's got to be dozens of ways to do this; this is just one way that should work in many shells.
  #3 (permalink)  
Old 11-22-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Your problem is you are looking in the wrong place. ;-)

The shell construct "if [ ...] ; then" is utilizing a command external to the shell: /usr/bin/test. In fact "[....]" is just an alternative way to write "test ...." and branch on the errorlevel (return value) of this command.

You have the following (basic) options in test, more to be found on the manpage of test:

(for the following we assume x=5 and y=10)
-gt "greater then" compare (integer) values: test $x -gt $y => returns FALSE
-ge "greater or equal"
-lt "lower then"
-le "lower equal"
-eq "equal"
-ne "not equal"

-a logical AND. Example: test "$x -gt 0 -a $y -ge x" => returns TRUE
-o logical OR. Analogous to above

\(...\) grouping. Example: test "\( $x -gt 0 \) -a \( $y -gt $x -o $y -gt 0 \)"

-n non-zero TRUE if a string is not empty
-z zero TRUE if a string is empty ("")

I hope this helps.

bakunin
  #4 (permalink)  
Old 11-22-2007
jadionne jadionne is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 39
Quote:
Originally Posted by bakunin View Post
Your problem is you are looking in the wrong place. ;-)

The shell construct "if [ ...] ; then" is utilizing a command external to the shell: /usr/bin/test. In fact "[....]" is just an alternative way to write "test ...." and branch on the errorlevel (return value) of this command.

You have the following (basic) options in test, more to be found on the manpage of test:

(for the following we assume x=5 and y=10)
-gt "greater then" compare (integer) values: test $x -gt $y => returns FALSE
-ge "greater or equal"
-lt "lower then"
-le "lower equal"
-eq "equal"
-ne "not equal"

-a logical AND. Example: test "$x -gt 0 -a $y -ge x" => returns TRUE
-o logical OR. Analogous to above

\(...\) grouping. Example: test "\( $x -gt 0 \) -a \( $y -gt $x -o $y -gt 0 \)"

-n non-zero TRUE if a string is not empty
-z zero TRUE if a string is empty ("")

I hope this helps.

bakunin


This looks a bit more streight forward to me...but again do you have a detailed link that can clear up some questions for me.

For example
Code:
test $x -gt $y => returns FALSE
what is the => representing here?
  #5 (permalink)  
Old 11-22-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Quote:
Originally Posted by jadionne View Post
This looks a bit more streight forward to me...but again do you have a detailed link that can clear up some questions for me.
No need for a link, just enter "man test" on your commandline and be enlightened. ;-))

Quote:
Originally Posted by jadionne View Post
For example
Code:
test $x -gt $y => returns FALSE
what is the => representing here?
Nothing. I just explained above, that in my example i assume x to be 5 and y to be 10. And if you ask "is x greater than y" this would return a logical FALSE. You could test this by entering the following on your commandline:

Code:
x=5
y=10
test $x -gt $y ; echo $?
test $y -gt $x ; echo $?
Line 3 would print "1", which is a logical "FALSE" and line 4 would print a "0", which is a logical TRUE.

bakunin
  #6 (permalink)  
Old 11-22-2007
jadionne jadionne is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 39
Quote:
Originally Posted by LivinFree View Post
It does appear that you're using SQL syntax in the shell

What shell do you use? What platform? (uname -a; echo $SHELL)

The syntax of `if` varies by shell and version, somewhat - here's an example that should work in bash, ksh, or POSIX shell (make sure to test it first!):
Code:
if [ $EXTRACT_ROW_COUNT > 0 -a $EXTRACT_ROW_COUNT = $LOADED_ROW_COUNT ]; then
     nohup $REFERENCE_SCRIPT"renme_ref_concern.sh" &
   else
     echo "LOAD INCOMPLETE" > $REFERENCE_LOG"CONCERN_LOAD_FAILURE.LOG"
fi
There's got to be dozens of ways to do this; this is just one way that should work in many shells.

To answer your question:
Code:
$ $ uname -a
SunOS scrbtpcdkbry211 5.10 Generic_118833-36 sun4u sparc SUNW,Sun-Fire-V490
$ echo $SHELL
/bin/ksh
Do you know of a good web page that discribes the switches to your command well? I beleve that is what is confusing me the most.
  #7 (permalink)  
Old 11-22-2007
jadionne jadionne is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 39
Quote:
Originally Posted by LivinFree View Post
It does appear that you're using SQL syntax in the shell

What shell do you use? What platform? (uname -a; echo $SHELL)

The syntax of `if` varies by shell and version, somewhat - here's an example that should work in bash, ksh, or POSIX shell (make sure to test it first!):
Code:
if [ $EXTRACT_ROW_COUNT > 0 -a $EXTRACT_ROW_COUNT = $LOADED_ROW_COUNT ]; then
     nohup $REFERENCE_SCRIPT"renme_ref_concern.sh" &
   else
     echo "LOAD INCOMPLETE" > $REFERENCE_LOG"CONCERN_LOAD_FAILURE.LOG"
fi
There's got to be dozens of ways to do this; this is just one way that should work in many shells.
One more question, If I am using the above code, how would I put in a second action if the test fails to terminate the script? Do I just use a ; after the log file is wrten and say end?
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 09:17 AM.


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