【问题标题】:email shell script part 2电子邮件外壳脚本第 2 部分
【发布时间】:2014-03-18 15:18:34
【问题描述】:

我正在创建一个 shell 脚本,它使用邮件功能接收用户输入和文本的人。我希望让它更先进。现在它一次只能给一个人发短信,我希望它能够通过用户输入“全部”来给多人甚至每个人发短信。

#!/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

另外,有没有更简洁的方法来引入一个包含数字而不是脚本的外部 txt 文件? (注意:我们仍然有 bash

【问题讨论】:

    标签: shell email scripting


    【解决方案1】:

    这里是重写。 在 bash3 中应该可以正常工作。

    #!/bin/bash
    
    # Prefix the numbers with something
    names=()
    names+=(Joe); numberJoe=8881235555
    names+=(Bob); numberBob=8881235556
    
    domain=txt.att.example.com
    
    usage () {
        echo "usage: $(basename $0) names message ..."
        echo "where: names is a comma-separated list of names (no spaces)"
        echo
        echo "example: $(basename $0) Jim,Fred hello lads, this is my message"
    }
    
    while getopts ":hl" opt; do
        case $opt in
            h) usage; exit ;;
            l) IFS=,; echo "known names: ${names[@]}"; exit ;;
        esac
    done
    shift $((OPTIND - 1))
    
    if (( $# < 2 )); then
        usage
        exit
    fi
    
    IFS=, read -ra usernamelist <<<"$1" 
    shift
    message="$*"
    
    # validate names
    namelist=()
    for name in "${usernamelist[@]}"; do
        if [[ " ${names[@]} " == *" $name "* ]]; then
            namelist+=("$name")
        else
            echo "unknown name: $name" >&2
        fi
    done
    if (( ${#namelist[@]} == 0 )); then
        echo "no valid names given" >&2
        exit 1
    fi
    
    # generate the recipient list
    echo "texting '$message' to:"
    recipients=()
    for name in "${namelist[@]}"; do
        numvar="number$name"
        echo "   $name -> ${!numvar}"
        recipients+=( "${!numvar}@$domain" )
    done
    
    # send it
    printf "%s\n" "$message" | mailx -s "Text Message via email" "$(IFS=,; echo "${recipients[*]}")"
    

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多