Problem with select statement

Hi

I have run out of ideas as to why this select doesn't work in a script I am writing.

The script sources a file of common functions and I am trying to use a select statement within one of the functions -

                                
PS3="Try? "
select X in CONT EXIT; do
    if [[ -n $X ]]
    then    
         M_USER_SELECTION=$X
         break
    fi
done

When I look at the output I see the prompt is drawn twice, which suggests that the loop is taking input from somewhere and finding it wanting, so is prompting again?

OP -

1) CONT
2) EXIT
Try? Try? 

I have tried every variation. I can get this to work without problem in simple test scripts. At present, no matter what I try, the script drops straight through the loop and the variable isn't set as the user never gets a chance to do anything.

Anyone suggest what I am doing wrong?

Cheers

Brad

PS this is BASH on Ubuntu Linux

Sorry, seems to work as expected on Mac OS X.

> select X in CONT EXIT; do     if [[ -n $X ]];     then              M_USER_SELECTION=$X;          break;     fi; done
1) CONT
2) EXIT
Try? 1
> echo $X
CONT

> select X in CONT EXIT; do     if [[ -n $X ]];     then              M_USER_SELECTION=$X;          break;     fi; done
1) CONT
2) EXIT
Try? 2
> echo $X
EXIT

Hi drPete

Yes, like I said, I too can get it to work in other scripts.

In fact if I source this same file of functions and call the function with the menu in, it works?!?

I'm wondering why I am getting the prompt twice, that seems to be the only symptom. Have I messed up the environment somehow? I'm definitely not calling the function twice, but I'm getting the prompt twice after setting PS3....

I'm baffled.

---------- Post updated at 04:13 PM ---------- Previous update was at 03:43 PM ----------

I have replaced the select statement with a while read -e statement and the loop drops straight through like the select does.

I'm beginning to suspect that the code is not reading from standard in but maybe I have an open file descriptor within the code somewhere, 2000+ lines....

Are there any debug commands that let me look at what is being read from?

I know that there is a named pipe being held open by a background child process and I'm wondering if the read is being affected by this??

---------- Post updated at 04:48 PM ---------- Previous update was at 04:13 PM ----------

I think I'm getting somewhere now. The script runs multiple jobs via ssh on remote servers and then exits after they have completed.

Looking at the ps output and the lsof for the user I see a lot of open ssh connections -

/home/brad/wip/tmp >lsof | grep sshtest
ssh        1071    sshtest  cwd   unknown                             /proc/1071/cwd (readlink: Permission denied)
ssh        1071    sshtest  rtd   unknown                             /proc/1071/root (readlink: Permission denied)
ssh        1071    sshtest  txt   unknown                             /proc/1071/exe (readlink: Permission denied)
ssh        1071    sshtest NOFD                                       /proc/1071/fd (opendir: Permission denied)
ssh        2858    sshtest  cwd   unknown                             /proc/2858/cwd (readlink: Permission denied)
ssh        2858    sshtest  rtd   unknown                             /proc/2858/root (readlink: Permission denied)
ssh        2858    sshtest  txt   unknown                             /proc/2858/exe (readlink: Permission denied)
ssh        2858    sshtest NOFD                                       /proc/2858/fd (opendir: Permission denied)
su         3290    sshtest  cwd   unknown                             /proc/3290/cwd (readlink: Permission denied)
su         3290    sshtest  rtd   unknown                             /proc/3290/root (readlink: Permission denied)
su         3290    sshtest  txt   unknown                             /proc/3290/exe (readlink: Permission denied)
su         3290    sshtest NOFD                                       /proc/3290/fd (opendir: Permission denied)
bash       3304    sshtest  cwd   unknown                             /proc/3304/cwd (readlink: Permission denied)
bash       3304    sshtest  rtd   unknown                             /proc/3304/root (readlink: Permission denied)
bash       3304    sshtest  txt   unknown                             /proc/3304/exe (readlink: Permission denied)
bash       3304    sshtest NOFD                                       /proc/3304/fd (opendir: Permission denied)
ssh        4508    sshtest  cwd   unknown                             /proc/4508/cwd (readlink: Permission denied)
ssh        4508    sshtest  rtd   unknown                             /proc/4508/root (readlink: Permission denied)
ssh        4508    sshtest  txt   unknown                             /proc/4508/exe (readlink: Permission denied)
ssh        4508    sshtest NOFD                                       /proc/4508/fd (opendir: Permission denied)
ssh        6089    sshtest  cwd   unknown                             /proc/6089/cwd (readlink: Permission denied)
ssh        6089    sshtest  rtd   unknown                             /proc/6089/root (readlink: Permission denied)
ssh        6089    sshtest  txt   unknown                             /proc/6089/exe (readlink: Permission denied)
ssh        6089    sshtest NOFD                                       /proc/6089/fd (opendir: Permission denied)
ssh       11851    sshtest  cwd   unknown                             /proc/11851/cwd (readlink: Permission denied)
ssh       11851    sshtest  rtd   unknown                             /proc/11851/root (readlink: Permission denied)
ssh       11851    sshtest  txt   unknown                             /proc/11851/exe (readlink: Permission denied)
ssh       11851    sshtest NOFD                                       /proc/11851/fd (opendir: Permission denied)
ssh       13499    sshtest  cwd   unknown                             /proc/13499/cwd (readlink: Permission denied)
ssh       13499    sshtest  rtd   unknown                             /proc/13499/root (readlink: Permission denied)
ssh       13499    sshtest  txt   unknown                             /proc/13499/exe (readlink: Permission denied)
ssh       13499    sshtest NOFD                                       /proc/13499/fd (opendir: Permission denied)
ssh       15217    sshtest  cwd   unknown                             /proc/15217/cwd (readlink: Permission denied)
ssh       15217    sshtest  rtd   unknown                             /proc/15217/root (readlink: Permission denied)
ssh       15217    sshtest  txt   unknown                             /proc/15217/exe (readlink: Permission denied)
ssh       15217    sshtest NOFD                                       /proc/15217/fd (opendir: Permission denied)
ssh       16830    sshtest  cwd   unknown                             /proc/16830/cwd (readlink: Permission denied)
ssh       16830    sshtest  rtd   unknown                             /proc/16830/root (readlink: Permission denied)
ssh       16830    sshtest  txt   unknown                             /proc/16830/exe (readlink: Permission denied)
ssh       16830    sshtest NOFD                                       /proc/16830/fd (opendir: Permission denied)
ssh       18468    sshtest  cwd   unknown                             /proc/18468/cwd (readlink: Permission denied)
ssh       18468    sshtest  rtd   unknown                             /proc/18468/root (readlink: Permission denied)
ssh       18468    sshtest  txt   unknown                             /proc/18468/exe (readlink: Permission denied)
ssh       18468    sshtest NOFD                                       /proc/18468/fd (opendir: Permission denied)
ssh       20109    sshtest  cwd   unknown                             /proc/20109/cwd (readlink: Permission denied)
ssh       20109    sshtest  rtd   unknown                             /proc/20109/root (readlink: Permission denied)
ssh       20109    sshtest  txt   unknown                             /proc/20109/exe (readlink: Permission denied)
ssh       20109    sshtest NOFD                                       /proc/20109/fd (opendir: Permission denied)
ssh       21715    sshtest  cwd   unknown                             /proc/21715/cwd (readlink: Permission denied)
ssh       21715    sshtest  rtd   unknown                             /proc/21715/root (readlink: Permission denied)
ssh       21715    sshtest  txt   unknown                             /proc/21715/exe (readlink: Permission denied)
ssh       21715    sshtest NOFD                                       /proc/21715/fd (opendir: Permission denied)
ssh       23417    sshtest  cwd   unknown                             /proc/23417/cwd (readlink: Permission denied)
ssh       23417    sshtest  rtd   unknown                             /proc/23417/root (readlink: Permission denied)
ssh       23417    sshtest  txt   unknown                             /proc/23417/exe (readlink: Permission denied)
ssh       23417    sshtest NOFD                                       /proc/23417/fd (opendir: Permission denied)
ssh       27084    sshtest  cwd   unknown                             /proc/27084/cwd (readlink: Permission denied)
ssh       27084    sshtest  rtd   unknown                             /proc/27084/root (readlink: Permission denied)
ssh       27084    sshtest  txt   unknown                             /proc/27084/exe (readlink: Permission denied)
ssh       27084    sshtest NOFD                                       /proc/27084/fd (opendir: Permission denied)
ssh       28696    sshtest  cwd   unknown                             /proc/28696/cwd (readlink: Permission denied)
ssh       28696    sshtest  rtd   unknown                             /proc/28696/root (readlink: Permission denied)
ssh       28696    sshtest  txt   unknown                             /proc/28696/exe (readlink: Permission denied)
ssh       28696    sshtest NOFD                                       /proc/28696/fd (opendir: Permission denied)
ssh       30497    sshtest  cwd   unknown                             /proc/30497/cwd (readlink: Permission denied)
ssh       30497    sshtest  rtd   unknown                             /proc/30497/root (readlink: Permission denied)
ssh       30497    sshtest  txt   unknown                             /proc/30497/exe (readlink: Permission denied)
ssh       30497    sshtest NOFD                                       /proc/30497/fd (opendir: Permission denied)
ssh       31992    sshtest  cwd   unknown                             /proc/31992/cwd (readlink: Permission denied)
ssh       31992    sshtest  rtd   unknown                             /proc/31992/root (readlink: Permission denied)
ssh       31992    sshtest  txt   unknown                             /proc/31992/exe (readlink: Permission denied)
ssh       31992    sshtest NOFD                                       /proc/31992/fd (opendir: Permission denied)
ps -ef | grep sshtest
sshtest   1071     1  0 15:58 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="16906" P_USR_TAG="demo" P_SYS_TAG="130331155809" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_20756" muse m_manual_test
sshtest   2858     1  0 16:02 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="13417" P_USR_TAG="demo" P_SYS_TAG="130331160240" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_18881" muse m_manual_test
sshtest   3290  2961  0 10:03 pts/3    00:00:00 su - sshtest
sshtest   3304  3290  0 10:03 pts/3    00:00:00 -su
sshtest   4508     1  0 16:05 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="2141" P_USR_TAG="demo" P_SYS_TAG="130331160539" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_18597" muse m_manual_test
sshtest   6089     1  0 16:06 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="18055" P_USR_TAG="demo" P_SYS_TAG="130331160623" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_28294" muse m_manual_test
brad      7357  6455  0 16:44 pts/9    00:00:00 grep --color=auto sshtest
sshtest  11851     1  0 10:07 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="1128" P_USR_TAG="demo" P_SYS_TAG="130331100705" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_21725" muse m_manual_test
sshtest  13499     1  0 10:11 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="20008" P_USR_TAG="demo" P_SYS_TAG="130331101111" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_2287" muse m_manual_test
sshtest  15217     1  0 10:15 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="7592" P_USR_TAG="demo" P_SYS_TAG="130331101524" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_30628" muse m_manual_test
sshtest  16830     1  0 10:17 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="555" P_USR_TAG="demo" P_SYS_TAG="130331101723" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_27671" muse m_manual_test
sshtest  18468     1  0 10:24 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="14212" P_USR_TAG="demo" P_SYS_TAG="130331102414" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_657" muse m_manual_test
sshtest  20109     1  0 10:28 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="16383" P_USR_TAG="demo" P_SYS_TAG="130331102824" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_12896" muse m_manual_test
sshtest  21715     1  0 10:29 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="32080" P_USR_TAG="demo" P_SYS_TAG="130331102917" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_29130" muse m_manual_test
sshtest  23417     1  0 10:39 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="3192" P_USR_TAG="demo" P_SYS_TAG="130331103913" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_31802" muse m_manual_test
sshtest  27084     1  0 15:33 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="4061" P_USR_TAG="demo" P_SYS_TAG="130331153330" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_32060" muse m_manual_test
sshtest  28696     1  0 15:37 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="19551" P_USR_TAG="demo" P_SYS_TAG="130331153726" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_14906" muse m_manual_test
sshtest  30497     1  0 15:55 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="16958" P_USR_TAG="demo" P_SYS_TAG="130331155539" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_10463" muse m_manual_test
sshtest  31992     1  0 15:57 pts/3    00:00:00 ssh sshtest@tx5xn  P_RND="16945" P_USR_TAG="demo" P_SYS_TAG="130331155728" P_JOB_ID="2" P_MASTER_HOST="ubuntu-dt64" P_MSG_PIPE="2-m_msg_data_10774" muse m_manual_test

I guess most of these are because I have been debugging the script and killing the script before it cleans up on exit. But I do wonder if there is a way of explicitly closing these connections and I still don't know if they are at the root of the problem of the read ...

---------- Post updated at 06:03 PM ---------- Previous update was at 04:48 PM ----------

I was correct to think it was reading from somewhere. I short circuited the function with this loop that explicitly reads from stdin -

        echo "Choose -"
        while read -e X <&2
        do
                case $X in
                        1)M_USER_SELECTION=CONTINUE;break;;
                        2)M_USER_SELECTION=EXIT;break;;
                        *);;
                esac
        done
return

Now it blocks on the read instead of dropping straight through.

I guess I should try and see where the select is reading from ...

---------- Post updated at 06:12 PM ---------- Previous update was at 06:03 PM ----------

Well, the "Fix" if it can be called that was to -

m_present_menu()
{
        [[ $# -eq 0 ]] && m_fail 1 "Error: Usage (${FUNCNAME})"

        local OPTIONS=$(cat "${1}" | tr "\n" " ")
        PS3="Please select a response - "
        select ACTION in ${OPTIONS} 
        do
                if [[ -n ${ACTION} ]]
                then
                        M_USER_SELECTION=${ACTION}
                        break
                fi
        done <&2
}

Although I would have preferred to understand where it was reading from without the explicit direction to read from stdin

Difficult to say without seeing the rest of your code. I suspect you're using this in some sort of loop with a file redirected into it, which 'select' takes to be stdin instead of the terminal...

If available on your system, execute lsof -p$$ just in front of your loop to see where stdin is redirected to.