thunderbird -compose "attachment='$HOME/test test.txt'"
有效。
thunderbird -compose "attachment='$HOME/test, test.txt'"
无效并给出file does not exist
错误消息。
这肯定是因为 Thunderbird 处理命令行参数的方式;例如,
thunderbird -compose "to='[email protected]',attachment='~/file.txt'"
参数compose
之间用 分隔,
,因此文件名中出现 肯定是,
会破坏程序的原因。但是,我想不出在文件名中“转义”逗号的方法。
笔记:
- 在 Thunderbird 3+ 中,
file://
不再需要使用该协议。
两个都
thunderbird -compose "attachment='$HOME/test test.txt'"
和
thunderbird -compose "attachment='file://$HOME/test test.txt'"
工作。
两者都不
thunderbird -compose "attachment='$HOME/test, test.txt'"
也不
thunderbird -compose "attachment='file://$HOME/test, test.txt'"
作品。
答案1
使用 @Etan Reisner 建议的对文件名进行 URL 编码的想法(在对我上述问题的评论中),我破解了一个解决方案,我在这里记录下来,以造福社区中的其他人。下面的代码片段${filename}
是要附加的完整文件名(包括路径)。
#!/bin/bash
# "A safe way to URL encode is to encode every single byte, even
# those that would've been allowed." This is done by first
# converting every byte to its 2-byte hex code using `hexdump' and
# then adding the prefix `%' to each 2-byte hex code pair using
# `sed'.
#
# Attribution: https://stackoverflow.com/a/7506695
filenameurlencoded=$(echo -ne "${filename}" | \
hexdump -v -e '/1 "%02x"' | \
sed 's/\(..\)/%\1/g')
filenamenopath="$(basename ${filename})"
emailsubject="${filenamenopath}"
emailbody="\"${filenamenopath}\" is attached."
emailattachment="file:///${filenameurlencoded}"
# Syntax of -compose command line option: double-quotes enclose
# full comma-separated list of arguments passed to -compose,
# whereas single quotes are used to group items for the same
# argument.
#
# Attribution: http://kb.mozillazine.org/Command_line_arguments_(Thunderbird)
thunderbird -compose "subject='${emailsubject}',body='${emailbody}',attachment='${emailattachment}'"