2

I have a script that runs a python script (used to deduplicate emails via IMAP) after a user has entered a servername, username & password. The first part of it gets a list of folders which the second part then uses as inputs - all fine unless a folder name has a space in it at which point the entry needs to be in quotation marks As an example Inbox is fine whereas My Inbox needs to be read as "My Inbox"

#!/bin/sh
# Delete all duplicate messages in all folders of said account.
# Note that we connect through SSL (-x) to the default port.

read -p "Server: " SERVER
read -p "Username: " USER
read -s -p "Password: " PASS
echo
echo ...........
echo FOLDER LIST
echo ...........
echo

# Next line generates list for display only

/tmp/IMAPdedup-master/imapdedup.py -s $SERVER -x -u $USER -w $PASS -l

# Next line generates list to be used by the do line - this is the entries that need 
to have each line in quotations

for folder in `/tmp/IMAPdedup-master/imapdedup.py -s $SERVER -x -u $USER -w $PASS -l`;

do /tmp/IMAPdedup-master/imapdedup.py -s $SERVER -x -u $USER -w $PASS $folder

done
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
lippyrich
  • 23
  • 2
  • 1
    Do they really need to be quoted - or do they just need to be not split? See [Why does my shell script choke on whitespace or other special characters?](http://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters) – steeldriver May 16 '16 at 10:06
  • No the need to be quoted as it will go down the list happily processing any folders without spaces (I presume this is a whitespace ?) in their names - including subfolders. – lippyrich May 16 '16 at 10:08

1 Answers1

4

You don't need the names to be quoted, you need the variables to be quoted. Specifically, to make your script work, you need to replace the for folder in ... loop with:

/tmp/IMAPdedup-master/imapdedup.py -s "$SERVER" -x -u "$USER" -w "$PASS" -l | 
while IFS= read -r folder; do 
 /tmp/IMAPdedup-master/imapdedup.py -s "$SERVER" -x -u "$USER" -w "$PASS" "$folder"
done

I suggest you read the following posts to understand why:

terdon
  • 234,489
  • 66
  • 447
  • 667