Shell脚本 - 基于用户输入的电子邮件脚本

时间:2014-03-03 23:49:51

标签: shell email unix text

我想创建一个电子邮件脚本,它实际上是一个使用邮件-x并发送到数字的短信脚本。它会是这样的:

#/bin/sh

Joe=8881235555
Bob=8881235556

echo "Who do you want to text?:(i.e. Joe, Bob, etc)"
read name
echo "What do you want to say?:)"
read quote
echo "texting $name with $quote"

echo $variablequote | mailx -s "Text Message via email" $variablename@txt.att.net

如何将用户输入名称转移到预设值?

1 个答案:

答案 0 :(得分:2)

考虑使用更现代的外壳:

#!/bin/bash

# Use an associative array, and map names to numbers
declare -A numbers
numbers=([Joe]=8881235555  [Bob]=8881235556)

echo "Who do you want to text?:(i.e. Joe, Bob, etc)"
read name
echo "What do you want to say?:)"
read quote

# Look up number by name
number=${numbers[$name]}

if [[ $number ]] 
then
  echo "texting $name ($number) with $quote"
  mailx -s "Text Message via email" "$number@txt.att.net" <<< "$quote"
else
  echo "Unknown user"
  exit 1
fi

如果你想使用/ bin / sh:

#!/bin/sh

# Prefix the numbers with something
number_Joe=8881235555
number_Bob=8881235556

echo "Who do you want to text?:(i.e. Joe, Bob, etc)"
read name
echo "What do you want to say?:)"
read quote

# Remove any dangerous characters that the user enters
sanitized=$(printf "%s" "$name" | tr -cd 'a-zA-Z')

# Look up by evaluating e.g. "number=$number_Joe"
eval "number=\$number_$sanitized"

if [ "$number" ] 
then
    echo "texting $name ($number) with $quote"
    printf "%s\n" "$quote" | mailx -s "Text Message via email" "$number@txt.att.net"
else
  echo "Unknown user"
  exit 1
fi