标题可能很模糊,但我有一个很好的例子:
echo "Test message:\nThis is a line.\nAnd this is another." | nail -s "`tail -1`" joe@localhost这里的目标是将回声的内容作为消息正文发送,并使用最后一行作为主题。然而,当我这样做的时候,我失去了身体。
echo "Test message:\nThis is a line.\nAnd this is another." | nail joe@localhost工作正常,但没有主题。
发布于 2013-01-02 19:57:52
您可以使用命名管道来完成此操作,这在以下位置有效:
mkfifo subj.fifo
echo "Test message:\nThis is a line.\nAnd this is another." |
tee >(tail -n1 > subj.fifo) | mail -s "$(< subj.fifo)" joe@localhost
rm subj.fifo注意:如果你使用头部而不是尾部,你需要让tee忽略SIGPIPE信号,例如trap '' PIPE。
发布于 2013-01-02 20:00:53
因为你的主题出现在最后一行,所以你必须缓冲所有的行(否则,就无法决定哪一行是最后一行)。将主题放在第一行会容易得多。管他呢。以下是一种可能的方法,使用bash4.0中出现的mapfile:
printf "%s\n" "Line one in the body of message" "Line two in the body of message" "Subject in the last line" | {
mapfile -t array
nail -s "${array[@]: -1}" joe@localhost < <(printf "%s\n" "${array[@]:0:${#array[@]}-1}")
}如果您决定将主题放在第一行,这就简单多了(当然,只需要一个管道,除了主题,没有无关的subshell或缓冲区):
printf "%s\n" "Subject in the first line" "Line one in the body of message" "Line two in the body of message" | { read -r subject; nail -s "$subject" joe@localhost; }发布于 2013-01-02 19:41:02
tail将丢弃最后一行之前的行。您可以使用临时文件,或者将主题放在第一位而不是最后。无论哪种方式,如果没有一个像tee一样的协作程序,管道就不可能既消耗又保持一条线路。
#!/bin/sh
# use first line as subject, args are recipients
# stdin is message body
read subj
( echo "$subj"; cat ) | nail -s "$subj" "$@"https://stackoverflow.com/questions/14121264
复制相似问题