Problem with select statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with select statement
# 1  
Old 03-31-2013
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 -

Code:
                                
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 -

Code:
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
# 2  
Old 03-31-2013
Sorry, seems to work as expected on Mac OS X.

Code:
> 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

# 3  
Old 03-31-2013
Thanks but...

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 -

Code:
/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)

Code:
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 -

Code:
        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 -

Code:
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
# 4  
Old 03-31-2013
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...
# 5  
Old 03-31-2013
If available on your system, execute lsof -p$$ just in front of your loop to see where stdin is redirected to.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing passing and executing select statement in loop

Hi, i want to do the following: Grep the following kind of strings for the 15digit ID which is stored in filename1: "14:06:51.396 INFO BMCREMEDYSD INPUT-ACTION Failed to retrieve Remedy Incident Modification record: 000000000039047 org.apache.axis2.AxisFault: Read timed out - complete... (9 Replies)
Discussion started by: Khushbu
9 Replies

2. Shell Programming and Scripting

SQLPLUS command with more than 1 select statement

Hi all, I'm using below code processId=`sqlplus -s ${sysuser}/${syspwd} <<CHK_PROCESS whenever sqlerror exit sql.sqlcode; set head off feedback off echo off pages 0 SELECT PROCESS_ID FROM LSHADMIN.DATA_DOMAIN WHERE DOMAIN_NAME = '${tabname}' ... (8 Replies)
Discussion started by: Pratiksha Mehra
8 Replies

3. Shell Programming and Scripting

Parse SQL text and only format first SELECT statement.

Hi Forum. Need your expertise on the following question. I have the following file which I would like to parse, find first block of SELECT statment and concatenate all input fields as 1 field (~ delimited): Old File: SELECT /*+ USE_HASH(CCOMM ICAR IMAP IAS IP IMAS IMPS IAP SPCA) */ ... (5 Replies)
Discussion started by: pchang
5 Replies

4. Shell Programming and Scripting

for each value in an array, execute select statement

Hello All, I am new to shell scripting. I am working on Solaris O/S, bash script and sybase programming. I want to loop through multiple values in an array and for each value, I want to select a row from the database. following is the code written for it. output="loop.csv" ... (8 Replies)
Discussion started by: arundhati_s
8 Replies

5. Shell Programming and Scripting

Select variable within a if statement

i want to select a variable created and use it in a if statement, but not getting the desired results LINE='device for 0101a01: lpd://172.25.41.111:515' prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7` echo $prt My if statement to select just what i want.. IFS=$":" while read prt... (11 Replies)
Discussion started by: ggoliath
11 Replies

6. Shell Programming and Scripting

How can i assign an select statement into a variable?

I am trying to assign an select statement into a variable. Can someone hel me with this. example : a='select * from dual' echo $a should give me select * from dual But this is not working. I trying with \ before * and quotes too. (1 Reply)
Discussion started by: rdhanek
1 Replies

7. Shell Programming and Scripting

using SELECT sql statement in shell script

Hi there I have a database on a remote box and i have been using shell script to insert data into it for example, i could have a script that did this SN=123456 n=server1 m=x4140 sql="UPDATE main SET hostname='$n',model='$m' WHERE serial='$SN';" echo $sql |/usr/sfw/bin/mysql -h... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

8. UNIX and Linux Applications

Oracle Select IN statement

If I recall, when I used informix I could do a sql statement like: SELECT Value from Table WHERE ID in (100,200,300); How do I do this in Oracle? I believe I am using Oracle 10 if that matters. Thanks. (1 Reply)
Discussion started by: benefactr
1 Replies

9. Windows & DOS: Issues & Discussions

Want to use the output of Select statement in Unix script

Hi, I have a UNIX script which calls SQL Select statement: Now i want to use the output of that select statement within my UNIX script so as to call different shell script depending upon the output of the select statement. Can anyone help me in this regard. TIA Akhil Goel (4 Replies)
Discussion started by: akhilgoel9
4 Replies

10. UNIX for Dummies Questions & Answers

Pipe SQL select statement results to script

Hello I would like to perform a select from a oracle table and return those values to my shell script For example: site=head -1 $infile | cut -c1-15 | awk '{printf "s%", $0} sqlplus -s /nolog |& #Open pipe to sql select col1, col2, col3, col4 from oracle_table where col5 =... (6 Replies)
Discussion started by: houtakker
6 Replies
Login or Register to Ask a Question