```bash
#!/bin/bash
# Function to lookup IP address for a domain
lookup_domain() {
local domain=$1
echo "Domain: $domain"
host $domain | grep "has address" | sed 's/.*has address /IP Address: /'
echo "-----------------------------"
}
# Function to display menu and handle user input
display_menu() {
while true; do
echo "Please select the option you want to proceed:"
echo "1) Input a domain name"
echo "2) Quit from the program"
read -p "Enter your choice (1 or 2): " choice
case $choice in
1)
read -p "Enter a domain name: " domain
lookup_domain $domain
;;
2)
echo "Exiting program. Goodbye!"
exit 0
;;
*)
echo "Invalid choice. Please enter 1 or 2."
;;
esac
done
}
# Main program
echo "Welcome to DNS Lookup Script"
if [ $# -eq 0 ]; then
display_menu
elif [ $# -eq 1 ]; then
if [ -f "$1" ]; then
while IFS= read -r domain || [[ -n "$domain" ]]; do
lookup_domain $domain
done < "$1"
else
echo "Error: File '$1' not found."
exit 1
fi
else
echo "Error: Too many parameters entered. Please provide only one filename or no parameters."
exit 1
fi
```
This script does the following:
1. It defines a `lookup_domain` function to perform DNS lookup for a given domain.
2. It defines a `display_menu` function to handle the interactive menu when no file is provided.
3. In the main program:
- If no arguments are provided, it runs the interactive menu.
- If one argument is provided, it treats it as a filename and processes each domain in the file.
- If more than one argument is provided, it displays an error message.
4. The script provides user-friendly messages and error handling throughout.
To use this script:
1. Save it as `dns_lookup.sh`
2. Make it executable with `chmod +x dns_lookup.sh`
3. Run it with `./dns_lookup.sh` for interactive mode or `./dns_lookup.sh filename.txt` to process a file.