Q:

Bash find string postion in list of strings

Whats the best way to achieve the following?
I want my script to find out which postion the host its running on is in a list of hosts.
thisHost=$(hostname)

machineList="pc02 server03 host01 server05"

So if thisHost=host01 I would get back postion 3. 
The machine list would never be contain more than 10 items.
I could do a comparison  match in a for loop but would like to know if there is a better way ?

A:

If you need to check many hosts, you can prepare a better structure for faster lookup: an associative array:
#! /bin/bash
machineList='pc02 server03 host01 server05'
declare -A machine_number
i=1
for machine in $machineList ; do
    machine_number[$machine]=$((i++))
done

thisHost=host01
echo ${machine_number[$thisHost]}

You can also use external tools:
read n _rest < <(echo "$machineList"
                 | tr ' ' '\n'
                 | nl
                 | grep -Fw "$thisHost")
echo $n

A:

Bash solution:
thisHost="host01"
machineList="pc02 server03 host01 server05"
machineListArr=($machineList)

for i in "${!machineListArr[@]}"; do 
    [ "$thisHost" = "${machineListArr[$i]}" ] && echo "position: $((i+1))"
done

The output:
position: 3

