Format of 'select' generated menu


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format of 'select' generated menu
# 15  
Old 04-21-2014
Quote:
I like to know answers, too. However, some questions are more important to me than others -- we all set priorities.
True enough.

You've pointed me into layers I've never been to before, but am not afraid to explore. I'll chew on that and consider this thread to have run its course. Thanks.
# 16  
Old 04-21-2014
Hi.

It is not easy to understand code that has been extracted from a larger source. Here is a shell script that drives a portion of the bash-select-code, but rendered in perl so that you can run it. It calculates the rows and columns as bash would for the select. It does not pretty-print the selections, it just does the calculations. It assumes that you have a file that corresponds to the strings you would use with select. It allows a second parameter, the setting for COLUMNS so that you can see the effects of changing that. Here are a few runs with varying files and widths, the last with some arithmetic and logical internal results displayed. Note that the shell script calls the perl script, so that it is easily posted here, however, you can copy/paste the perl code and run it as desired.
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate driver for bash select row-col calculations

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

FILE=${1-data1}

pl " Results, $FILE:"
./p2 $FILE 80

FILE=data2
pl " Results, $FILE:"
./p2 $FILE 80

pl " Results, $FILE 20:"
./p2 $FILE 20

pl " Results, $FILE 80, details:"
./p2 -d $FILE 80

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
perl 5.10.0

-----
 Results, data1:

For data file data1, containing:
fsaaaaa
fsbbbbb
fsccccc
hrddddd
 Maximum element length = 7

 Final calculation, COLUMNS (width) = 80:
 Rows = 4
 Cols = 1

-----
 Results, data2:

For data file data2, containing:
fsaaaaa
fsbbbbb
fsccccc
hrddddd
None of the above
 Maximum element length = 17

 Final calculation, COLUMNS (width) = 80:
 Rows = 2
 Cols = 3

-----
 Results, data2 20:

For data file data2, containing:
fsaaaaa
fsbbbbb
fsccccc
hrddddd
None of the above
 Maximum element length = 17

 Final calculation, COLUMNS (width) = 20:
 Rows = 5
 Cols = 1

-----
 Results, data2 80, details:
debug: value, filename, COLUMNS = data2, 80
debug: value, array a length 5 "fsaaaaa fsbbbbb fsccccc hrddddd None of the above", longest = 17
debug: value initial cols = 4
debug: value before  , rows, cols = 2, 4
debug: value after   , rows, cols = 2, 3
debug: value adjusted, rows, cols = 2, 3

For data file data2, containing:
fsaaaaa
fsbbbbb
fsccccc
hrddddd
None of the above
 Maximum element length = 17

 Final calculation, COLUMNS (width) = 80:
 Rows = 2
 Cols = 3

And here it the perl code, p2:
Code:
#!/usr/bin/env perl

# @(#) p2	Demonstrate rows, cols calculations for bash "select".

use warnings;
use strict;

# Short circuit for debug.
# Use with d(), keys: Action, Location, Value.
our ($debug);
$debug = 0;
if ( @ARGV && $ARGV[0] =~ /-de*b*u*g*/ ) {
  $debug = 1;
  shift;
}

# Read list, calculate longest line.

my ( $filename, $COLUMNS, $f, $longest, $t1, @a, @list );
my ( $list_len, $max_elem_len, $cols, $rows, $string );

$filename = shift || "data1";
$COLUMNS  = shift || "80";
d("value, filename, COLUMNS = $filename, $COLUMNS");

open( $f, "<", $filename ) || die "Cannot open file $filename\n";

$longest = 0;
while (<$f>) {
  chomp;
  $t1 = length($_);
  $longest = $t1 > $longest ? $t1 : $longest;
  push @a, $_;
}
$list_len = scalar(@a);
d("value, array a length $list_len \"@a\", longest = $longest");
@list         = @a;
$max_elem_len = $longest;

$cols = $max_elem_len ? $COLUMNS / $max_elem_len : 1;
$cols = int($cols);
d("value initial cols = $cols");
$cols = 1 if $cols == 0;

$rows = $list_len ? $list_len / $cols + ( $list_len % $cols != 0 ) : 1;
$rows = int($rows);

d("value before  , rows, cols = $rows, $cols");
$cols = $list_len ? $list_len / $rows + ( $list_len % $rows != 0 ) : 1;
$cols = int($cols);
d("value after   , rows, cols = $rows, $cols");

if ( $rows == 1 ) {
  $rows = $cols;
  $cols = 1;
}
d("value adjusted, rows, cols = $rows, $cols");

print "\n";
print "For data file $filename, containing:\n";
foreach $string (@list) {

  print "$string\n";
}
print " Maximum element length = $max_elem_len\n";
print "\n";

print " Final calculation, COLUMNS (width) = $COLUMNS:\n";
print " Rows = $rows\n";
print " Cols = $cols\n";

exit;

sub d {

  # Debug - print strings if debug does not fail.
  if ( not $debug ) {
    return;
  }
  elsif ( scalar(@_) == 0 ) {
    print "\n";
  }
  else {
    print STDERR join "", "debug: ", @_, "\n";
  }
}

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 17  
Old 04-22-2014
Quote:
Originally Posted by edstevens
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$

COLUMNS might be getting set in /etc/profile or ~/.bashrc or something similar.
# 18  
Old 04-27-2014
Quote:
Originally Posted by Corona688
COLUMNS might be getting set in /etc/profile or ~/.bashrc or something similar.
Is it meaningfull to set COLUMNS hardcoded to any and every terminal(-window) just so select looks 'pretty'?
AFAIK all the rest of the terminal output depends on that too, as in line breaks and such.
# 19  
Old 04-27-2014
Quote:
Originally Posted by edstevens
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$

Perhaps COLUMNS is not exported, try export COLUMNS before running doit2

If COLUMNS is not set scripts/programs could be using tput cols or similar to query the terminal for it's actual width.

Also see bash shopt checkwinsize, this keeps COLUMNS and LINES updated automatically, this is useful for an xterm where the users could resize the window after the shell has started.

Last edited by Chubler_XL; 04-27-2014 at 10:09 PM..
# 20  
Old 04-30-2014
Quote:
Originally Posted by sea
Is it meaningful to set COLUMNS hardcoded to any and every terminal(-window) just so select looks 'pretty'?
That's a rather loaded question. I don't even know which layout you consider "prettiest" yet.

No, you should not hardcode COLUMNS to make select prettier. Doing this may cause inconsistent, unreliable, or unreadable results across different systems, terminals, and shells.

If the end user wants to hardcode their COLUMNS that's another matter. They'd be the one to know how big their terminal is.
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