Warning! I'm ridiculously new at all this, so pardon my ignorance...
I have a very simple script which is intended to search a hosts file when given a partial hostanme or ip address. The if the partial hostname/ip given is unique, the script automatically logs the user in to that host. If there are multiple matches, a numerical list of all matches is presented as a menu. Selecting the number that represents the intended host then logs the user in.
The script works like a charm if the results (the menu) only shows the host name or the ip address. I want it to show both. However, when it shows both, the login fails.
The menu portion of the script is as follows:
ip=$1
test=$(cat /etc/hosts | grep -c $ip)
if [ $test -eq 1 ]; then
clogin `awk '/'"$ip"'/ {print $2}' /etc/hosts `
else
options=$"`awk '/'"$ip"'/ {print $1 "--" $2}' /etc/hosts`"
echo
echo "There were $test matches for '$ip' found"
echo
echo "Please select the site you would like to login to:"
echo
select opt in $options;do
echo " "
clogin $opt
exit 1
done
When run, the result is as expected:
./test springs
There were 3 matches for 'springs' found
Please select the site you would like to login to:
1) 10.1.1.2--hostname_with_springs1
2) 10.1.1.3--hostname_with_springs2
3) 10.1.1.4--hostname_with_springs3
Of course, then, selecting any of those fails because of the dashes. The menu is delimted via white-space so, having any spaces between the IP and hostname would create a menu selection for each (giving six matches to the above query). :wall:
How can I have my cake and eat it too?
Thank you in advance for any help on this!