0

I want to create a email script that really is a texting script that is using the mail -x and sending to numbers. It would go something like this:

#/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" [email protected]

How do I get that user input name to transfer in to a preset value?

1 Answer 1

2

Consider using a more modern shell:

#!/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" "[email protected]" <<< "$quote"
else
  echo "Unknown user"
  exit 1
fi

If you want to use /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" "[email protected]"
else
  echo "Unknown user"
  exit 1
fi
Sign up to request clarification or add additional context in comments.

3 Comments

in bash, can also use indirect variables ${!name}@example.com but that is not readable, hard to maintain
@glennjackman, it's worth documenting here, though, for a user trying to support bash 2.x or 3.x (as associative arrays are 4.x-only).
Thi was very helpful! Now, how would I include a All function? Is it possible to pull the numbers in from a seperate txt file instead of setting them in the script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.