Saturday, July 23, 2022

[SOLVED] bash select loop does not wait for input

Issue

I'm trying to make my life easier deploying something on 80+ machines. However not all of those machines run the same version of RHEL and there are subtle differences in the scripts that need to be run on them.

That's why I want get the release version first, and then choose which file to deploy on the machine.

The problem is the select loop won't wait for my input, it skips it completely and just moves on.

Here's the code I'm using

#!/bin/bash

version() {
    cat /etc/*-release | grep "release"
    select ver in "6" "7"
    do
        case $ver in
            6) echo "install for RH6"; break;;
            7) echo "install for RH7"; break;;
        esac
    done
}

while read srv
do
    ping -c 1 $srv
    ssh -n $srv "$(typeset -f); version"
done < $1

The ping is just there for test purposes

here's some sample output:

[email protected]'s password:
Red Hat Enterprise Linux Server release 7.1 (Maipo)
Red Hat Enterprise Linux Server release 7.1 (Maipo)

1) 6
2) 7
#? 

Solution

the select can't read your input, because the input is already connected to the file $1 (done < $1 on the last line). Or actually the EOF or something is read.

If you need to interact with the script, you need to rewrite the while loop to something different, for example

for srv in "localhost"

works fine.



Answered By - Jakuje
Answer Checked By - Dawn Plyler (WPSolving Volunteer)