0

I want to read a text file line by line and below is the shell script which I used.I am getting the exception as below :

fileReader.sh: line 8: syntax error near unexpected token done' 'ileReader.sh: line 8:done < $FILE

Script used

echo "Start"
FILE="/abc/def/ghi/Sample.TXT"
echo "File Path -----"$FILE
while read line;do
    echo "Line # : $line";
done < $FILE
Jason Roman
  • 8,146
  • 10
  • 35
  • 40
  • `'ileReader.sh: line 8: \`done ` in an error message sure looks like an embedded CR character. Run `cat -vet fileReader.sh` and see if there are any `^M` characters. – Mark Plotnick Mar 16 '16 at 17:02
  • on running that , I got the below response : – user1670805 Mar 16 '16 at 18:49
  • $ cat -vet fileReader.sh #!/bin/bash^M$ echo "Start"^M$ file= "/opt/hp/ccdm/var/outbound/Auto_Stand_GS_List.TXT"^M$ echo "File Path -----"$FILE^M$ k=0^M$ while read -r line^M$ do^M$ ((k=$k+1))^M$ echo "Line # $k: $line"^M$ done < "$file"^M$ echo "Total number of lines in file: $k"^M$ – user1670805 Mar 16 '16 at 18:49
  • OK. This happens with files created on Windows and then copied over to Unix. You can use the [dos2unix](http://linux.die.net/man/1/dos2unix) program to get rid of those CR characters, or use your favorite Unix text editor. More details in this SO question: [Convert DOS line endings to Linux line endings in vim](http://stackoverflow.com/questions/82726/convert-dos-line-endings-to-linux-line-endings-in-vim) – Mark Plotnick Mar 16 '16 at 18:55
  • I removed it by using your suggestion ,but again I am facing the below problem . ]$ sh fileReader1.sh Start This File Path ----/opt/hp/ccdm/var/outbound/Auto_Stand_GS_List.TXT : No such file or directoryt/hp/ccdm/var/outbound/Auto_Stand_GS_List.TXT End this – user1670805 Mar 16 '16 at 19:41
  • That path is not wrong ,if I do a cat operation with that path.I am able to read it ,but this program is not working . Please suggest. – user1670805 Mar 16 '16 at 19:42
  • The overlapping characters in that error message, `or directoryt/hp/ccdm`, may indicate there's still a stray CR there. – Mark Plotnick Mar 16 '16 at 19:56

0 Answers0