Help with if-then-else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with if-then-else
# 1  
Old 11-01-2005
Help with if-then-else

Does anyone see something wrong with this statement? I am getting a "syntax error at line 30 : `else' unmatched" error.

Code:
dayofweek=`date +%w"-"%a`
echo "DayofWeek  = " $dayofweek

if [ $dayofweek -eq "1-Mon" ] ; then
      logdir=/opt/batch/$1/logs/$dayofweek
else if [ $dayofweek -eq "2-Tue" ] ; then
      logdir=/opt/batch/$1/logs/$dayofweek
else if [ $dayofweek -eq "3-Wed" ] ; then
      logdir=/opt/batch/$1/logs/$dayofweek
else if [ $dayofweek -eq "4-Thu" ] ; then
      logdir=/opt/batch/$1/logs/$dayofweek
else if [ $dayofweek -eq "5-Fri" ] ; then
      logdir=/opt/batch/$1/logs/$dayofweek
else if [ $dayofweek -eq "6-Sat" ] ; then
      logdir=/opt/batch/$1/logs/$dayofweek
else if [ $dayofweek = "7-Sun" ] ; then
      logdir=/opt/batch/$1/logs/$dayofweek
fi;

# 2  
Old 11-01-2005
There are only seven days in a week. No matter which day it is, you seem to do the same thing. That does not make sense. But "else if" should be "elif" provided that I correctly guessed at which shell you're using. And that trailing semicolon is superfluous.
# 3  
Old 11-01-2005
I have logfile directories for each day of the week. I'm using Korn shell.
# 4  
Old 11-01-2005
Quote:
Originally Posted by ssmiths001
I have logfile directories for each day of the week. I'm using Korn shell.
But you have a variable set to the day of the week!
logdir=/opt/batch/$1/logs/$dayofweek # on Monday you use this
logdir=/opt/batch/$1/logs/$dayofweek # on Tuesday you use this
It is the same command being executed on each day of the week. The value in $dayofweek is changing and that is all that needs to change.
# 5  
Old 11-01-2005
OK, now I understand what you are saying. Thanks for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question