bash 修改脚本以从 echo 命令获取参数
bash script modify to take parameter from echo command
我有 this small bash script(sendmail.sh) to send emails using mandril which works super when used like this ./sendmail.sh "my@email.com" "Email Subject" "Email body"
. Thanks to black @ LET 但是我希望此脚本像 linux 邮件命令一样从 echo 命令获取其电子邮件正文。 echo "email body" | mail -s "email subject" email@any.com
当我将下面的脚本与此命令一起使用时 echo "email body" |./sendmail.sh "my@email.com" "Email Subject"
它会打印 else 块中指定的错误(因为只给出了 2 个参数但需要 3 个)
/sendmail.sh requires 3 arguments - to address, subject, content
Example: ././sendmail.sh "to-address@mail-address.com" "test" "hello this is a test message"
惊讶地发现为什么 echo
命令输出未被用作脚本中 $3 参数的输入。
#!/bin/bash
#created by black @ LET
#MIT license, please give credit if you use this for your own projects
#depends on curl
key="" #your maildrill API key
from_email="" #who is sending the email
reply_to="$from_email" #reply email address
from_name="curl sender" #from name
if [ $# -eq 3 ]; then
msg='{ "async": false, "key": "'$key'", "message": { "from_email": "'$from_email'", "from_name": "'$from_name'", "headers": { "Reply-To": "'$reply_to'" }, "return_path_domain": null, "subject": "''", "text": "''", "to": [ { "email": "''", "type": "to" } ] } }'
results=$(curl -A 'Mandrill-Curl/1.0' -d "$msg" 'https://mandrillapp.com/api/1.0/messages/send.json' -s 2>&1);
echo "$results" | grep "sent" -q;
if [ $? -ne 0 ]; then
echo "An error occured: $results";
exit 2;
fi
else
echo "[=11=] requires 3 arguments - to address, subject, content";
echo "Example: ./[=11=] \"to-address@mail-address.com\" \"test\" \"hello this is a test message\""
exit 1;
fi
为什么这令人惊讶?您正在混合参数和标准输入,这在根本上是完全不同的。
不过,满足这个要求并不难。
case $# in
3) text="" ;;
2) text=$(cat) ;;
esac
: .... do stuff with "$text"
您的脚本的缩进和引号有点草率,所以这里是一个稍微重构的版本。
key="" #your maildrill API key
from_email="" #who is sending the email
from_name="curl sender" #from name
case $# in
3) text="";;
2) text="$(cat)";;
*) echo "[=11=]: oops! Need 2 or 3 arguments -- aborting" >&2; exit 1 ;;
esac
msg='{ "async": false, "key": "'"$key"'", "message": { "from_email": "'"$from_email"'", "from_name": "'"$from_name"'", "return_path_domain": null, "subject": "'""'", "text": "'"$text"'", "to": [ { "email": "'""'", "type": "to" } ] } }'
result=$(curl -A 'Mandrill-Curl/1.0' -d "$msg" 'https://mandrillapp.com/api/1.0/messages/send.json' -s 2>&1)
case $results in
*"sent"*) exit 0;;
*) echo "[=11=]: error: $results" >&2; exit 2;;
esac
请特别注意任何用户提供的字符串绝对必须在双引号内。
(我把Reply-To:
去掉了,因为当它等于From:
头的时候就完全多余了。)
我有 this small bash script(sendmail.sh) to send emails using mandril which works super when used like this ./sendmail.sh "my@email.com" "Email Subject" "Email body"
. Thanks to black @ LET 但是我希望此脚本像 linux 邮件命令一样从 echo 命令获取其电子邮件正文。 echo "email body" | mail -s "email subject" email@any.com
当我将下面的脚本与此命令一起使用时 echo "email body" |./sendmail.sh "my@email.com" "Email Subject"
它会打印 else 块中指定的错误(因为只给出了 2 个参数但需要 3 个)
/sendmail.sh requires 3 arguments - to address, subject, content
Example: ././sendmail.sh "to-address@mail-address.com" "test" "hello this is a test message"
惊讶地发现为什么 echo
命令输出未被用作脚本中 $3 参数的输入。
#!/bin/bash
#created by black @ LET
#MIT license, please give credit if you use this for your own projects
#depends on curl
key="" #your maildrill API key
from_email="" #who is sending the email
reply_to="$from_email" #reply email address
from_name="curl sender" #from name
if [ $# -eq 3 ]; then
msg='{ "async": false, "key": "'$key'", "message": { "from_email": "'$from_email'", "from_name": "'$from_name'", "headers": { "Reply-To": "'$reply_to'" }, "return_path_domain": null, "subject": "''", "text": "''", "to": [ { "email": "''", "type": "to" } ] } }'
results=$(curl -A 'Mandrill-Curl/1.0' -d "$msg" 'https://mandrillapp.com/api/1.0/messages/send.json' -s 2>&1);
echo "$results" | grep "sent" -q;
if [ $? -ne 0 ]; then
echo "An error occured: $results";
exit 2;
fi
else
echo "[=11=] requires 3 arguments - to address, subject, content";
echo "Example: ./[=11=] \"to-address@mail-address.com\" \"test\" \"hello this is a test message\""
exit 1;
fi
为什么这令人惊讶?您正在混合参数和标准输入,这在根本上是完全不同的。
不过,满足这个要求并不难。
case $# in
3) text="" ;;
2) text=$(cat) ;;
esac
: .... do stuff with "$text"
您的脚本的缩进和引号有点草率,所以这里是一个稍微重构的版本。
key="" #your maildrill API key
from_email="" #who is sending the email
from_name="curl sender" #from name
case $# in
3) text="";;
2) text="$(cat)";;
*) echo "[=11=]: oops! Need 2 or 3 arguments -- aborting" >&2; exit 1 ;;
esac
msg='{ "async": false, "key": "'"$key"'", "message": { "from_email": "'"$from_email"'", "from_name": "'"$from_name"'", "return_path_domain": null, "subject": "'""'", "text": "'"$text"'", "to": [ { "email": "'""'", "type": "to" } ] } }'
result=$(curl -A 'Mandrill-Curl/1.0' -d "$msg" 'https://mandrillapp.com/api/1.0/messages/send.json' -s 2>&1)
case $results in
*"sent"*) exit 0;;
*) echo "[=11=]: error: $results" >&2; exit 2;;
esac
请特别注意任何用户提供的字符串绝对必须在双引号内。
(我把Reply-To:
去掉了,因为当它等于From:
头的时候就完全多余了。)