find . -name 'Email*.log' -exec grep -il 'bad password' {} \;| while read line; do
  TEMPNUM=`tail -20 $line | grep 'bad password' | wc -l`
  if [ $TEMPNUM -gt 0 ]; then
     awk '/Username/{print $NF}' $line >> expiredmailbox.txt

This is the code I used and ran the shell script. I am getting
line 6: syntax error near unexpected token `done' Even if just try to echo the $line. I am getting an error. How can I eliminate this?

  • hmm the syntax looks ok - your script seems to work for me apart from the awk (works if I replace with echo "$line") (you probably want to add a -type f to your find command to stop grep throwing errors and quote your "$line" variable but I don't think those things are your problem). Are you sure you copied your script here exactly??
    – Zanna
    Dec 2, 2016 at 9:28
  • In which shell you are working, is it perhaps bash? Assuming you need loops at all it would make things quite a bit easier.
    – phk
    Dec 2, 2016 at 9:40
  • 1

1 Answer 1


can you try this..


find . -name 'Email*.log' | xargs grep -li  'bad password' |  while read line
  TEMPNUM=$(tail -20 "${line}" | grep -c 'bad password')
  if [ "${TEMPNUM}" -gt "0" ]; then
     awk '/Username/{print $NF}' "${line}" >> expiredmailbox.txt
  • Still the same error
    – AmrutaMV
    Dec 7, 2016 at 8:24
  • if you run with bash -x script.sh what is the output.. where it shows the error ? what is your OS
    – Kamaraj
    Dec 7, 2016 at 10:06

You must log in to answer this question.

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