[newb] simple script, big problem

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!

Just change IFS (input field seperator) to a comma and delimit your list with comma:

ip=$1
test=$(grep -c $ip /etc/hosts)
if [ $test -eq 1 ]; then
  clogin $(awk '/'"$ip"'/ {print $2}' /etc/hosts)
else
  options=$(awk '/'"$ip"'/ {printf $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
  ( IFS=,
    select opt in $options
    do
      echo " "
      clogin ${opt%% *}
      exit 1
    done )
fi
1 Like

That worked like a charm. Now, I wonder how much studying it will take before I understand why. Lol.

*man printf

Thanks a million!!

---------- Post updated at 08:06 PM ---------- Previous update was at 06:41 PM ----------

Ok. My brain is completely fried (eyes are literally burning...told you I was a newb.) Lol.

I'd like to put in an error check. If they choose a menu # that is > than $test or null, then echo a message.

If opt is blank they didn't select a valid option

if [ -z "$opt" ]
then
    echo "Invalid choice!"
else
   ....
fi

Hmm.. if I put in a number too high, it jumps to

clogin ${opt%% *}

If null, then it just repeats the menu. Doesn't seem to echo the error either way.

What shell are you using, below is working fine under bash:

options="10.1.1.2 hostname_with_springs1,10.1.1.3 hostname_with_springs2,10.1.1.4 hostname_with_springs3"
  ( IFS=,
    select opt in $options
    do
      if [ -z "$opt" ]
      then
          echo "Invalid choice"
      else
          echo " "
          echo "You choose ${opt%% *}"
          break
      fi
    done )
1 Like

Not sure what the issue was before, but it's working fine now.
Thanks again!