If Then Else Logic


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If Then Else Logic
# 1  
Old 11-21-2007
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  
Old 11-22-2007
It does appear that you're using SQL syntax in the shell Smilie

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  
Old 11-22-2007
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  
Old 11-22-2007
Quote:
Originally Posted by LivinFree
It does appear that you're using SQL syntax in the shell Smilie

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.
# 5  
Old 11-22-2007
Quote:
Originally Posted by bakunin
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?
# 6  
Old 11-22-2007
Quote:
Originally Posted by LivinFree
It does appear that you're using SQL syntax in the shell Smilie

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?
# 7  
Old 11-22-2007
Quote:
Originally Posted by jadionne
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
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help in logic

I have big large snapshot file which contains every process start time and end time. One server snapshot contains many Application handle. My task is to identify the process id for which the end time is not there or empty also if the completion time is not on the same date then I need to kill. ... (6 Replies)
Discussion started by: senthilkumar_ak
6 Replies
Login or Register to Ask a Question