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

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?

It does appear that you're using SQL syntax in the shell :slight_smile:

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

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.

Your problem is you are looking in the wrong place. :wink:

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

To answer your question:

$ $ 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.

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

test $x -gt $y => returns FALSE

what is the => representing here?

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?

No need for a link, just enter "man test" on your commandline and be enlightened. ;-))

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:

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

I'm not sure I understand 100%, but I read it to mean "Can I run more than one command after "else?"'

Yes. Here's a pseudocode example:

if [ $something == $somethingelse ]; then
    echo "something equals somethingelse"
    run_another_command_or_function
    return 0
else
    echo "something is not equal to somethingelse! "
    run_yet_another_command
    run_yet_another_another_command
    return 1
fi

Being that it's ksh (and depending on whether it's ksh88 or ksh93,) you can even write that differently. Here's one That would work with the latest ksh:

((something==somethingelse)) && {
    echo "something equals somethingelse"
    run_another_command_or_function
    return 0
  } || {
    echo "something is not equal to somethingelse! "
    run_yet_another_command
    run_yet_another_another_command
    return 1
}

Like I said, there's many ways to do it - check the man page for ksh to see the capabilities and syntax.

One more thing - in a modern shell, running 'if [ something ]' is not equivalent to running /bin/test. The '[', '[[', '((', etc., commands are typically built in the to the shell, meaning you do not search $PATH nor do you necessarily fork() to run these commands. They're internal to the shell.

The options to /bin/test are likely the same as those in your shell, though, so the man page is of use, just as moderns shells have a built-in 'printf' command, even though a binary exists on disk with similar options/functions (for compatibility reasons.)