Format of 'select' generated menu


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format of 'select' generated menu
# 8  
Old 04-16-2014
Many commands and programs interacting with terminals try to find out the terminal geometry by several means. The COLUMNS environment variable is one of those, containing the width of the terminal. select then tries to fit as many menu items across as would fit into the terminal width. If you reduce that artificially, it will print just one item across.
# 9  
Old 04-16-2014
That makes sense, but I'm still can't seem to get my head around how that works in the specifics of this case.

If I shorten my literal last entry to simply 'None', and don't specifically set COLUMNS:
Code:
oracle:11g$ cat doit
#!/bin/sh
echo
ORACLE_SID=''
PS3='Target (test) database: '
#export COLUMNS=20
echo COLUMNS = $COLUMNS
#
while [[ $ORACLE_SID = "" ]]; do
  select ORACLE_SID in `egrep -i '^FS|^HR' /etc/oratab |\
      awk -F\: '{print $1}'|sort` 'None'; do
    if [[ $ORACLE_SID = "" ]]; then
         echo
         echo "Please enter a valid number.  Retry.";
         echo
    elif [[ $ORACLE_SID = 'None of the above' ]]; then
         exit ;
    else {
          break ;
         }
    fi
    break
    done
done
#
unset PS3
exit

oracle:11g$ doit

COLUMNS =
1) fs91dvvb
2) fs91uavb
3) fs9devvb
4) hr91tsvb
5) None
Target (test) database:

But if I simply lengthen that literal;
Code:
#!/bin/sh
echo
ORACLE_SID=''
PS3='Target (test) database: '
#export COLUMNS=20
echo COLUMNS = $COLUMNS
#
while [[ $ORACLE_SID = "" ]]; do
  select ORACLE_SID in `egrep -i '^FS|^HR' /etc/oratab |\
      awk -F\: '{print $1}'|sort` 'None of the above'; do
    if [[ $ORACLE_SID = "" ]]; then
         echo
         echo "Please enter a valid number.  Retry.";
         echo
    elif [[ $ORACLE_SID = 'None of the above' ]]; then
         exit ;
    else {
          break ;
         }
    fi
    break
    done
done
#
unset PS3
exit

oracle:11g$ doit

COLUMNS =
1) fs91dvvb           3) fs9devvb           5) None of the above
2) fs91uavb           4) hr91tsvb
Target (test) database:

FWIW, note in both cases when the script echoed the value of $COLUMNS, it was null. But if I do it from the command line
Code:
oracle:11g$ echo $COLUMNS
80

oracle:11g$

I would have expected the script to have inherited the value of 80 from the process that launched it.

And finally with the longer literal menu item, but with COLUMNS explicitly set:

Code:
oracle:11g$ cat doit
#!/bin/sh
echo
ORACLE_SID=''
PS3='Target (test) database: '
export COLUMNS=20
echo COLUMNS = $COLUMNS
#
while [[ $ORACLE_SID = "" ]]; do
  select ORACLE_SID in `egrep -i '^FS|^HR' /etc/oratab |\
      awk -F\: '{print $1}'|sort` 'None of the above'; do
    if [[ $ORACLE_SID = "" ]]; then
         echo
         echo "Please enter a valid number.  Retry.";
         echo
    elif [[ $ORACLE_SID = 'None of the above' ]]; then
         exit ;
    else {
          break ;
         }
    fi
    break
    done
done
#
unset PS3
exit

oracle:11g$ doit

COLUMNS = 20
1) fs91dvvb
2) fs91uavb
3) fs9devvb
4) hr91tsvb
5) None of the above
Target (test) database:

# 10  
Old 04-16-2014
The output of all of those looks readable, is the thing. Neither too many lines, nor too wide, to be visible. Not 100% consistent when you give it different options and configurations, but that's kind of the point -- it's a command that's designed to automatically format itself to be readable, and so far you haven't managed to make it not be so. What, exactly, is actually wrong?
# 11  
Old 04-17-2014
Quote:
Originally Posted by Corona688
The output of all of those looks readable, is the thing. Neither too many lines, nor too wide, to be visible. Not 100% consistent when you give it different options and configurations, but that's kind of the point -- it's a command that's designed to automatically format itself to be readable, and so far you haven't managed to make it not be so. What, exactly, is actually wrong?
Yes, it's all readable and usable. Yes, the different options produce different results and that is to be expected. As I've stated earlier in the thread, what I'm after is some real understanding of exactly what the interactions are and how they work. I'm trying to learn something usable from this experience. I'm not the kind of guy who simply accepts "here, use this code, it works in this case". I've always been driven to peel back the layers and understand why it works the way it does so that I can apply that knowledge appropriately to other situations.

So, I don't understand why
- with the default setting of COLUMNS, the list with a short literal ('None') presented as a single column, but with a longer literal ('None of the above') it presented in three columns.

- setting the value of COLUMNS to 20 caused the previous 3 column list to be presented as one column.

- echoing $COLUMNS from the command prompt produced a value (80), but echoing it from within the shell script called from the same command prompt resulted in what appears to be nulls. I would have expected (and all of my previous experience confirms) that the shell script would have inherited the entire environment from the calling process. So I would expect the default value of COLUMNS within the script to be the same as the value at the command prompt that called the script.
# 12  
Old 04-17-2014
You mix up the defintion of columns.
While $COLUMNS represents the value of characters possible to be presented on one line,
the columns of 'select' contain more than 1 char per line.

While its true that a script inherits the values of variables from the caller-environmnet (eg: terminal),
you can set it to something diffrent and change it back after its use.
Example (in the script):
Code:
OLD_COL=$COLUMNS
COLUMNS=20
select CHOICE in A B C D E F G;do echo $CHOICE;break;done
COLUMNS=$OLD_COL

So you dont mix up the output that follows your 'select' call.

hth
# 13  
Old 04-21-2014
Quote:
Originally Posted by sea
You mix up the defintion of columns.
While $COLUMNS represents the value of characters possible to be presented on one line,
So if it set COLUMNS=20 it shouldn't present a menu in multiple columns if such presentation would require more than 20 characters (including spacing) per line? Again, that makes sense in explanation, but not in observation. With the default value of 80 (or is it null? see comment on that, regarding environment inheritance, below), and all of my menu items of short (< 8 chars each) it presented in a single column. But by the above understanding it should have presented multiple columns. Only when one of the menu items was a longer value did the presentation go to multiple values.

Quote:
the columns of 'select' contain more than 1 char per line.
I'm afraid the meaning of that is escaping me.

Quote:
While its true that a script inherits the values of variables from the caller-environmnet (eg: terminal),
you can set it to something diffrent and change it back after its use.
Example (in the script):
Code:
OLD_COL=$COLUMNS
COLUMNS=20
select CHOICE in A B C D E F G;do echo $CHOICE;break;done
COLUMNS=$OLD_COL

So you dont mix up the output that follows your 'select' call.

hth
Yes, I know I can save and reset it. But my point was that I was not observing the script to actually inherit from the caller.

Code:
oracle:11g$ cat doit2
#!/bin/sh
echo COLUMNS = $COLUMNS
exit

oracle:11g$ echo $COLUMNS
80

oracle:11g$ ./doit2
COLUMNS =

oracle:11g$ echo $COLUMNS
80

oracle:11g$

# 14  
Old 04-21-2014
Meta-answers

Hi, edstevens.

I like to know answers, too. However, some questions are more important to me than others -- we all set priorities.

So some of the ways to get answers for:
Quote:
Originally Posted by edstevens
That makes sense, but I'm still can't seem to get my head around how that works in the specifics of this case.
1) Keep asking the question here in hopes that someone will answer,

2) Put on your scientist hat, and run lots of experiments, trying to see the pattern of behavior for various inputs,

3) Put on your programmer hat, and look at the code.

In case you are interested in pursuing 3), the bash 4.2 has a routine sequence:
Code:
execute_cmd.c
  execute_select_command
    select_query
      print_select_list

The latter section of code takes into account many items of interest:
Code:
static void
print_select_list (list, list_len, max_elem_len, indices_len)
     WORD_LIST *list;
     int list_len, max_elem_len, indices_len;
{
  int ind, row, elem_len, pos, cols, rows;
  int first_column_indices_len, other_indices_len;

  if (list == 0)
    {
      putc ('\n', stderr);
      return;
    }

  cols = max_elem_len ? COLS / max_elem_len : 1;
  if (cols == 0)
    cols = 1;
  rows = list_len ? list_len / cols + (list_len % cols != 0) : 1;
  cols = list_len ? list_len / rows + (list_len % rows != 0) : 1;

  if (rows == 1)
    {
      rows = cols;
      cols = 1;
    }

  first_column_indices_len = NUMBER_LEN (rows);
  other_indices_len = indices_len;

  for (row = 0; row < rows; row++)
    {
      ind = row;
      pos = 0;
      while (1)
	{
	  indices_len = (pos == 0) ? first_column_indices_len : other_indices_len;
	  elem_len = print_index_and_element (indices_len, ind + 1, list);
	  elem_len += indices_len + RP_SPACE_LEN;
	  ind += rows;
	  if (ind >= list_len)
	    break;
	  indent (pos + elem_len, pos + max_elem_len);
	  pos += max_elem_len;
	}
      putc ('\n', stderr);
    }
}

static int
print_index_and_element (len, ind, list)
      int len, ind;
      WORD_LIST *list;
{
  register WORD_LIST *l;
  register int i;

  if (list == 0)
    return (0);
  for (i = ind, l = list; l && --i; l = l->next)
    ;
  fprintf (stderr, "%*d%s%s", len, ind, RP_SPACE, l->word->word);
  return (displen (l->word->word));
}

static void
indent (from, to)
     int from, to;
{
  while (from < to)
    {
      if ((to / tabsize) > (from / tabsize))
    {
      putc ('\t', stderr);
      from += tabsize - from % tabsize;
    }
      else
    {
      putc (' ', stderr);
      from++;
    }
    }
}

Best wishes ... cheers, drl

Last edited by drl; 04-21-2014 at 12:37 PM.. Reason: Missing chunk from bash code.
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stuck with menu in select

hi i am new to bash scripting .. i created a bunch of folders but only want the folder names with file1. so i go in and make an array to grab the folders the put in a file then i strip the directories so i just have the folder names i need then i would like to make the menu with a selection... (3 Replies)
Discussion started by: lamby22
3 Replies

2. Shell Programming and Scripting

Help with 'select' for menu input

A lot of my scripting makes use of the 'select' command to create menu driven input. A typical example of how I use it is as: somevar='' PS3='Select one: ' while ]; do select somevar in $(sqlplus -s $dbuser/$dbpw@mydb <<EOF set echo off feedback off verify off... (7 Replies)
Discussion started by: edstevens
7 Replies

3. Shell Programming and Scripting

File Select Menu Script

Hi All, I need to develop a bash script list “list of files” and able to select if any and set as globe variable in script and use for other function. I would like to see in menu format. Example out put Below are the listed files for database clone 1. Sys.txt 2. abc.txt 3. Ddd.txt... (1 Reply)
Discussion started by: ashanabey
1 Replies

4. Shell Programming and Scripting

Select ksh menu question

I am creating a Select menu with a few options and I would like to create a "better" looking interface than just this: 1) Option 1 2) Option 2 3) Option 3 Instead, I would like something like this: *********** * Cool Script * * 1) Option 1 * * 2) Option 2 * * 3) Option 3 *... (2 Replies)
Discussion started by: chipblah84
2 Replies

5. Shell Programming and Scripting

reprint the select menu after a selection

Hi, I am using a select in ksh for a script #!/bin/ksh FIRSTLIST='one two three four quit' PS3='Please select a number: ' select i in $FIRSTLIST ; do case $i in one) print 'this is one' ;; two) print 'this is 2' ;; three) print 'this is 3' ;; four) print... (7 Replies)
Discussion started by: omerzzz
7 Replies

6. Shell Programming and Scripting

Error using select menu command

Hi All, I am trying to use the select command & the menu. below mention is my script #!/bin/bash 2 3 PS3="Is today your birthday? " #PS3 system variable 4 5 echo "\n" 6 7 8 select menu_selection in YES NO QUIT 9 do 10 11 ... (1 Reply)
Discussion started by: milindb
1 Replies

7. Shell Programming and Scripting

Select command to build menu

Hello everyone. I am using the select command to build a menu, here is my question: Is it possible to generate a menu which contains several sections and have a separator between the sections without having a selection number generated in front of the separator? This is a sample of what I would... (1 Reply)
Discussion started by: gio001
1 Replies

8. Shell Programming and Scripting

reappearing menu list using select

is there a way I can make the menu list reappear when I use select ? ----- menulist="Change_title Remove_tag Change_tag Add_line Quit" select word in $menulist #change_title remove_tag change_tag add_line quit do case $word in # first menu option Change Title ... (9 Replies)
Discussion started by: forever_49ers
9 Replies

9. Shell Programming and Scripting

Drop down menu in bash for timezone select

Is there any way to implement a drop down menu selection in bash? This is on CDLinux which is a very minimal live CD and I am using it to install an image onto a hard drive. Part of that process is the timezone selection. There are just too many timezones to attempt to use the "select" command.... (1 Reply)
Discussion started by: simonb
1 Replies

10. Shell Programming and Scripting

dynamic Select menu

Hi all is menu driven by SELECT can be a dynamic ? My requirement is that i want SELECT to be created on run time not predefine . The select should be created as per the no of words in a file thanks in advance rawat (2 Replies)
Discussion started by: rawatds
2 Replies
Login or Register to Ask a Question