Bash (Ubuntu server): Syntax error: "|" unexpected in While-loop

Hello forum,

I hope my problem is easy to solve for someone in here!

My main task is to copy a large amount of imap-accounts from one server to another. There is a tool (Perl) called imapsync which does the job exellent. Unfortunately I'm only able to run it on one account at a time.

After some heavy googling I learned I could make a shell script that, combined with a csv-file, does the job for me. Well, it turns out that was easier said than done. (I am by no means experienced in shell-scripting. -In fact; I have never made any scripts before...)

What I want the script to do:
As long as there is unread information in the csv-file, I want it to copy the imap-account AND create a logfile for that spesific account.

What the script does:
It gives me the error Syntax error: "|" unexpected...

The script looks like this:

#! /bin/sh
while IFS=';' read  u1 u2 p2
	do 
		imapsync --buffersize 8192000 --nosyncacls --subscribe --syncinternaldates --debug --host1 mail.server.com --user1 "$u1" --authuser1 administrator@server.com --password1 Password --ssl1 --port1 993 --host2 mail.server.net --user2 "$u2" --password2 "$p2"  --authmech2 PLAIN --ssl2 --port2 993; | tee "$u1".log \
	done < userinfo.csv
exit

If anyone can tell me why this doesn't work and how I should solve it I would be very greatful!

Remove the ';' before the '|' and the '\' at the EOL...?

while IFS=';' read  u1 u2 p2
	do 
		imapsync --buffersize 8192000 --nosyncacls --subscribe --syncinternaldates --debug --host1 mail.server.com --user1 "$u1" --authuser1 administrator@server.com --password1 Password --ssl1 --port1 993 --host2 mail.server.net --user2 "$u2" --password2 "$p2"  --authmech2 PLAIN --ssl2 --port2 993 | tee "$u1".log
	done < userinfo.csv
exit

You've got ; | If you want to pipe, you shouldn't end in ;, because ; means "end this command and start a new one".

Also, that \ in the middle of the line probably isn't doing what you want. It probably was there originally to combine two lines into one but now it's combining a line you don't want to be part of the statement, "done", into the statement. Ditch it.

I'd reorganize it as this:

#! /bin/sh
while IFS=';' read  u1 u2 p2
do 
	imapsync --buffersize 8192000 --nosyncacls --subscribe \
                --syncinternaldates --debug --host1 mail.server.com --user1 "$u1" \
                --authuser1 administrator@server.com --password1 Password --ssl1 \
                --port1 993 --host2 mail.server.net --user2 "$u2" --password2 "$p2" \
                --authmech2 PLAIN --ssl2 --port2 993 |
                        tee "$u1".log
done < userinfo.csv
exit

Unbelievable. It works!

Thank you both so very much, this has been a real headache to me!
:slight_smile: