Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am getting syntax error near unexpected token done while executing my shell script:

while read filename
do
  echo "$filename"
  if [ -s $filename ]; then
    tail -10 $filename | grep `date '+%Y-%m-%d'` >> $lastlines1
    echo "- Next Error File - " >> $lastlines1
  done
  else
  echo " no errrors"
fi

Any ideas, where am I going wrong?

share
1  
some general comments: You probably want to quote any filename variables that might possibly contain spaces. –  Gert van den Berg Jan 2 '13 at 12:51

4 Answers 4

you are closing the while before the if.

while read filename 
do 
    echo "$filename" 
    if [ -s $filename ]
    then 
        tail -10 $filename | grep date '+%Y-%m-%d' >> $lastlines1 
        echo "- Next Error File - " >> $lastlines1 
    else 
        echo " no errrors" 
    fi
done
share

Let's add some new lines and indentation:

1 while read filename; do
2     echo "$filename"
3     if [ -s $filename ]; then
4         tail -10 $filename | grep date '+%Y-%m-%d' >> $lastlines1
5         echo "- Next Error File - " >> $lastlines1
6     done
7 else
8     echo " no errrors"
9 fi

lines 6 and 9 seem to be swapped. In other words the while-do-done and if-then-else-fi clauses are overlapping. Which is wrong in shell (and most many other computer languages).

share

You need to use vim editor for scripting , it will shows text in RED if that syntax wrong

while read FileName 
do 
        echo "${FileName}" 

        if [ -s "${FileName}" ]; then 
            tail -10 $FileName | grep "date '+%Y-%m-%d'" >> "${lastlines1}"
            echo "- Next Error File - " >> "${lastlines1}"
        else 
            echo " no errrors" 
        fi      
done
share
1  
vim won't magically fix code, just as another editor (except maybe nano without -w) won't break it... It does make finding syntax errors easier though... Installing vim on some non-Linux systems can be quite annoying... –  Gert van den Berg Jan 2 '13 at 12:32
    
i am not saying it will fix the code , but at least we can notice the same. isn't it ? –  Rahul Patil Jan 2 '13 at 12:34
    
It depends a lot on the user's environment. For a root user on a Linux system with internet access and a decent package manager, switching to vim is probably easy (if they can operate a vi-like editor)... Not so much for a normal user on Solaris or a user on an embedded system with limited space... –  Gert van den Berg Jan 2 '13 at 12:49

You need to complete the if statement before the do while. If you are trying to only echo no errors once if no files are found, you need to use a flag to indicate it.

errorCount=0
while read filename 
do 
    echo "$filename" 
    if [ -s $filename ]
    then 
        tail -10 $filename | grep date '+%Y-%m-%d' >> $lastlines1 
        echo "- Next Error File - " >> $lastlines1 
        errorCount=$(($errorCount + 1))
    fi
done
if [[ $errorCount -eq 0 ]]
then
    echo " no errors"
fi
share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .