Score:2

Syntax error in While loop

pn flag

Here is my code -

#!/bin/bash
find FileFrom -type f -name "*" > prodRefresh.txt
filename="prodRefresh.txt"
while read -r line; do
    name=$line
    echo "Name read from file - $name"
done < $filename

while trying to execute via command prompt, giving below error -

copyAndCreate.sh: line 7: syntax error near unexpected token `done'
copyAndCreate.sh: line 7: `done < $filename'

Please help me to solve this. Thanks

hr flag
Check your script for DOS-style (CRLF) line endings (ex. `file copyAndCreate.sh`)
Atul avatar
pn flag
@steeldriver why it is giving error to me
Score:4
cn flag

This will happen if your file has Windows style line endings (\r\n) instead of regular Unix ones (\n). You have probably edited this file in a Windows environment at some point, and that changed the line endings and the extra \r is messing with your script. The solution is to fix the line endings:

dos2unix copyAndCreate.sh

Or, if you don't have dos2unix, try:

sed -i 's/\r//' copyAndCreate.sh
Artur Meinild avatar
vn flag
It's always a good idea to edit Linux files on Linux - or at least use Notepad++, which let's you choose the type of line endings in the file.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.