You won't be able to retrieve email attachments using basic command lines. You'll have to use a more advanced script language (ex : Python), with an Imap library to download messages, parse them, and eventually save attachments to files.
The reason is that the messages downloaded from your Imap server are formatted according to the MIME RFC. The Imap fetch command will allow you to download the MIME part representing your attachment, but it will be encoded (usually in base64). So, once downloaded, you'll have to decode the attachment, before saving it to a file. All steps that will be dealt with transparently by a proper IMAP library :)