【问题标题】:Sending mail with subject as variable发送主题为变量的邮件
【发布时间】:2016-06-25 13:04:00
【问题描述】:

在这里,我想使用 unix shell 脚本发送邮件。

我使用以下脚本进行简单的邮寄:

echo 'Message body goes here' | mail -s 'subject line goes here' email@provider.com

注意:现在我想使用如下所示的变量发送主题来自其他文件的邮件。

文件 1

该文件包含以下脚本:

#!/bin/bash
subjectvariable="Subject is here" 
export $subjectvariable

我想得到这个$subjectvariable 应该在邮件命令中用作变量。

我的尝试

文件 2

#!/bin/bash
echo 'Message body goes here' | mail -s "$subjectvariable" email@provider.com

但在输出中我没有得到主题。

只是 echo 命令的变量工作文件,如下图所示:

【问题讨论】:

  • 我认为在第一个脚本中应该是export subjectvariable="Subject is here。注意:该变量仅可用于子进程。所以文件 2 必须从文件 1 运行。
  • 您需要使用 source ./setSubject.sh 获取主题脚本才能在当前 shell 中设置变量
  • @anubhava,我已经导出了那个变量。那么还需要将源文件放到当前的shell中吗?
  • Export 不能使其对父 shell 可用
  • @anubhava,好的。让我给你看点东西。只需查看我更新的帖子,请回复。

标签: bash shell email command-line command


【解决方案1】:

这应该可以工作

文件 1:

#!/bin/bash
subjectvariable="Subject is here" 
export subjectvariable=$subjectvariable

和源文件 1 在 exec 文件 2 之前

【讨论】:

  • 出现错误:source 未找到。当我插入source ./file1.sh.
  • 有点晚了,试试 source file.sh without ./ ./ is for exec not source
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
相关资源
最近更新 更多