1

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?

1
  • 1
    some general comments: You probably want to quote any filename variables that might possibly contain spaces. Jan 2, 2013 at 12:51

5 Answers 5

5

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
4

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 other computer languages).

1

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
3
  • 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... Jan 2, 2013 at 12:32
  • i am not saying it will fix the code , but at least we can notice the same. isn't it ? Jan 2, 2013 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... Jan 2, 2013 at 12:49
1

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
0

Sometimes this error happens because of unexpected CR characters in file, usually because the file was generated on a Windows system which uses CR line endings. You can fix this by running dos2unix or tr, for example:

tr -d '\015' < yourscript.sh > newscript.sh

This removes any CR characters from the file and in new shell script file you didn't get that error.

You must log in to answer this question.

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