我希望使用以下bash脚本简化发送带有固定正文消息的附件,
#!/bin/sh
echo "body of message" | mutt -s "subject" -a $(find /path/to/dir -type f -name "*$1*") -- $2 < /dev/null
但是,有时find命令会查找多个文件以供附件使用。有没有一种更具有互动性的方法来做到这一点?例如,如果它找到文件xyz.pdf和xyz2.pdf,我可以选择一个,然后继续发送文件吗?
发布于 2014-10-27 20:12:31
您可以将find
的输出传递给select
命令。它是一个循环,允许您从选项列表中反复选择一个项,并使用刚刚选定的值运行循环的主体。
select attachment in $(find /path/to/dir -type f -name "*$1*"); do
echo "body of message" | mutt -s "subject" -a "$attachment" -- "$2" < /dev/null
break # To avoid prompting for another file to send
done
这并不理想;如果它发现任何名字中有空格的文件,就会中断。您可以更加小心地构建文件列表(这超出了这个答案的范围),然后调用select
命令。例如:
# Two poorly named files and one terribly named file
possible=("file 1.txt" "file 2.txt" $'file\n3.txt')
select attachment in "${possible[@]}"; do
echo "body of message" | ...
break
done
发布于 2014-10-27 20:33:53
#!/bin/bash
function inter {
typeset file array i=0
while IFS= read -r -d $'\0' file; do
array[i]=$file
((i+=1))
done
if (( i == 0 )); then
echo "no file" >&2
exit 1
fi
if (( i == 1 )); then
echo "$array"
return
fi
select file in "${array[@]}"; do
if (( REPLY>=1 && REPLY<=i )); then
break
fi
done </dev/tty
echo "$file"
}
echo "body of message" | mutt -s "subject" -a "$(find /path/to/dir -type f -name "*$1*" -print0 | inter )" -- $2 < /dev/null
https://stackoverflow.com/questions/26595098
复制相似问题