If Then Else Logic


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If Then Else Logic
# 8  
Old 11-23-2007
Quote:
Originally Posted by jadionne
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?
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:
Code:
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:
Code:
((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.)
 
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