troubleshooting Question

unix korn shell issue with if/elif/elif/elif condition

Avatar of reuel3
reuel3 asked on
Unix OSLinux OS DevSystem Programming
14 Comments1 Solution14098 ViewsLast Modified:
When looping through this while loop, I get to the first if condition which happens to be true.  It should not get to any of the other elif conditions, right?  I'm looping through a file and if the line doesn't have a ';' at the end, I want to log a message and go to the next line.  

Also, can someone explain the significance of the brackets?  It behaves differently if I have one or two brackets around a condition.

cat $LOGGING_INFO_FILE | while read LINE
firstChar=`echo $LINE | cut -f1 -d "/"`
if [[ $firstChar = "."  ]]; then
  restOfLine=`echo $LINE | cut -f2 -d "."`
  echo " " >> $LOG_OUTPUT_FILE
  echo $restOfLine  >> $LOG_OUTPUT_FILE
  echo "*****************************************************************" >> $LOG_OUTPUT_FILE
  errorCodeLine=`echo $LINE | cut -f4 -d "," | cut -f1 -d ")"`
  errorCode=`echo $LINE | cut -f4 -d "," | cut -f2 -d "." | cut -f1 -d ")"`
  errorCodeFile=`echo $LINE | cut -f4 -d "," | cut -f1 -d "." | cut -f1 -d ")"`

  lastChar=`echo $LINE | tail -2c`
  commaCount=$((`echo "$LINE" | sed 's/[^,]//g' | wc -c` - 1 ))
  count=`grep -ic $errorCodeLine $LOGGING_INFO_FILE`

  if [[ $lastChar != ";" ]]; then
    echo "This file contains a carriage return in the TEHelper.logMessage().  Please remove all carriage returns." >> $LOG_OUTPUT_FILE
  elif [ commaCount -gt 3 ]; then
    echo "This file contains comma's in TEHelper.logMessage().  Please remove all comma's from first parameter" >> $LOG_OUTPUT_FILE
  elif [ $errorCodeFile != $ICNEC ]; then
    echo "INVALID ERROR CODE -->" $errorCode " is not valid" >> $LOG_OUTPUT_FILE
  elif [ $count -gt 1 ]; then
    echo "ERROR CODE IS USED " $count " times  -->" $errorCode >> $LOG_OUTPUT_FILE

Join our community to see this answer!
Unlock 1 Answer and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros