Re: Any one know how to use select command ??

From: Dan Mercer (dmercer_at_mn.rr.com)
Date: 10/01/05


Date: Sat, 01 Oct 2005 18:05:57 GMT


<ebrahimbandookwala@gmail.com> wrote in message news:1128148992.245295.114420@g43g2000cwa.googlegroups.com...
: Hi
: I am now posting the entire shell script
:
: #!/bin/ksh
:
: set -A Array #for holding entire file
: set -A flight #for holding individual items
: set -A tail
: set -A price
: set -A seats
: set -A first
: set -A business
:
: typeset -i C=0
: typeset -F price=0 #for reading price
:
: iep -E > data
: exec 0<data
:
:
: while getopts t: OPT
:
: do
:
: case $OPT in
: t) thresh=$OPTARG;; #threshold
: *) print "kdata2 -t [threshold] " # for price only
: exit 1;;
:
: esac
: done
:
: space="|" # custom delim
:
: while read flight tail price_e seats first bus
: do
: if [[ $price_e < $thresh ]]

Someone else has addressed your first problem - that you've redirected select's
stdin to your file. Since you are using "typeset -F" I will assume you are
using ksh93 and not pdksh or ksh88. in which case you can read data
directly into an array.

BTW, the above won't do what you think it does. [[ A < B ]] does a
left to right lexicographical comparison of two values:

    $ [[ 20 < 3 ]] && echo ok
    ok
    $ typeset -F a=10.3 b=2.45
    $ [[ $a < $b ]] && echo ok
    ok
    $ [[ $a -lt $b ]] || echo ok
    ok

Either use [[ a -lt b ]] or ((a<b))

Also, you can use the output field separator to place your own delimiter
in a string construct from array elements. The ofs is the first character in
the IFS.

    $ set -- 1 2 3 4 5
    $ set -A A -- a b c d e f
    $ IFS="|$IFS";echo "$*";echo "${A[*]}";IFS=${IFS#?}
    1|2|3|4|5
    a|b|c|d|e|f

The $* or ${array[*]} must be located in double quotes for an echo or print
statement, although a straight assignment does not require quotes.

    $ set -- 1 2 3 4 5
    $ IFS="|$IFS";x=$*;IFS=${IFS#?}
    $ echo $x
    1|2|3|4|5

You can also concatenate array elements by setting IFS= to null. In that case
you must save off the value of IFS.

IFS can be typeset local to function calls. Since it's dangerous to play with, I usually do it
in functions so I don't have to worry about resetting it:

(ksh88)
   function concat
      {
      typeset vname=${1:?VNAME required}IFS=
      shift
      eval $vname='$*'
      }

(ksh93)

   function concat
      {
      typeset -n vname=${1:?VNAME required}
      typeset IFS=
      shift
      vname=$*
      }

Of course you could easily modify this simple function to concatenate
using any character:

   function concat
      {
      typeset -n vname=${1:?VNAME required}
      typeset IFS=${2?Delimiter required - may be null}
      shift
      vname=$*
      }

Then you could call it as:

   concat PATH ":" /usr/bin /usr/local/bin /usr/X11/bin

or

   concat XYZ '' 1 2 3 4 5

Dan Mercer

: then
:
:
: Array[C]=$flight$space$tail$space$price_e$space$seats$space$first$space$bus$space
: #delim seperated values for select
: flight[C]=$flight
: tail[C]=$tail
: price[C]=$price_e
: seats[C]=$seats
: first[C]=$first
: business[C]=$bus
: fi
: ((C=C+1))
:
:
: done
:
: select option in "${Array[@]}"
: do
: echo $option
:
: for i in ${Array[@]}
: do
: C=0
: if [[ $option = ${Array[C]} ]]
: then
:
: echo ${flight[C]}
: fi
: done
: done
:
:
:
: Now the contents of
:
: iep -E > data
:
: JetBlue jb1000 120.339996 300 0 0
: Airtran at2002 230.339996 500 50 50
: BA ba1234 0 500 10 15
:



Relevant Pages

  • Re: What is wrong with the code fragment?
    ... You send in an array of objects, but it's never used, since you try to ... "flight" itself is the pointer to the ... shouldn't even try to replace the array, since you loose the reference to it ... SetupFlightDB flightDB = new SetupFlightDB; ...
    (comp.lang.java.programmer)
  • Re: Getting unique elements in a bash array
    ... I am trying to obtain the unique elements in a bash array. ... Ideally, I would like to modify 'array' itself with the result, and would like to do it more elegantly. ... why is the setting of IFS required? ...
    (comp.unix.shell)
  • RE: [PHP] Sorting arrays
    ... >> the new info to the same array, ... >> I'm sorting the arrays by the flight dates. ... >> Jake McHenry ... > which sort ...
    (php.general)
  • RE: [PHP] Sorting arrays
    ... >> the new info to the same array, ... >> I'm sorting the arrays by the flight dates. ... >> correct order? ... > in reverse order, he/she does it for the fourth and it's in ...
    (php.general)
  • Re: Field splitting: Is the default IFS treated differently?
    ... The term " IFS white space" is used to ... That's an area where POSIX shells differ from the Bourne shell. ... While fiddling with a script to read a file into an array with a single ...
    (comp.unix.shell)