I am trying to make a bash script that shows me the domains MX-es of one or more domains given as arguments when running it. Example script call:
./domains.sh domain1.com domain-no-mx.com domain2.net domain3.io
The expected output is each domain with a : followed by their MX-es - each on a new line and to make it readable 2 spaces at the beginning of each MX. If there is no MX, just output the domain Example output:
domain1.com:
mx1.domain1.com
mx2.domain1.com
domain-no-mx.com:
domain2.net:
mx1.domain2.net
mx2.domain2.net
domain3.io:
mx1.domain3.io
This is what I got so far:
#!/bin/bash
# Check if at least one domain (argument) has been given
if [ $# = 0 ]; then
echo "usage: $(basename $0) list-of-domains" 1>&2
exit 1
fi
for domain in "$@"
do
echo "$domain:"
MX=$(dig $@ MX +short | cut -d " " -f 2)
printf '%s\n' " ${MX[@]}"
# for (( i=0; i<${#arr[@]}; i++));
# do
# echo " ${arr[i]}"
# done
done
It works with one domain (except the thing with the 2 spaces at the beginning of each MX) but it repeats the MX-es when giving multiple domains (arguments). I'm really struggling with the output and also with the details. I also tried with a second for loop and array (commented it out). As you can see, I'm rather a beginner and would be happy to get some inputs here :)
Thank you so much, greetings Marco
for domain in "$@"
thendomain
gets each of the command line arguments in turn. You probably want to then use that fordig
, not$@
again.