Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2004
    Posts
    4

    Unanswered: positional parameters

    Im writing a script that will read the positional parameters and look for a particular key word. If it finds it, elements to the left of it will be put into an array and elements to the right will be put into a second array.
    Any thoughts how I can do this?

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try this script (which can be simplified for ksh and perhaps bash) :
    Code:
    #
    # Fill Array1 with parameters to the left of $KEYWORD
    #
    indx=0
    while [ $# -gt 0]
    do
        parm="$1"
        shift
        [ "$parm" = "$KEYWORD" ] && break
        Array1[$indx]="$parm"
        indx=`expr $indx + 1`
        shift
    done
    
    #
    # Fill Array2 with parameters to the right of $KEYWORD
    # With Ksh : set -A Array2 "$@"
    # With Bash : ?
    #
    indx=0
    while [ $# -gt 0 ]
    do
       Array2[$indx]="$1"
       shift
       indx=`expr $indx + 1`
    done
    Jean-Pierre.

  3. #3
    Join Date
    Jan 2004
    Posts
    4
    Thank you. I do have a question where in the second part are you checking for the Keyword so elements can be put into a string

    # Fill Array2 with parameters to the right of $KEYWORD
    # With Ksh : set -A Array2 "$@"
    # With Bash : ?
    #
    indx=0
    while [ $# -gt 0 ]
    do
    Array2[$indx]="$1"
    shift
    indx=`expr $indx + 1`
    done

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The keyword is tested in the first part (assume variable KEYWORD is set to the value of the keyword) :

    [ "$parm" = "$KEYWORD" ] && break

    At the start of the second part, the keyword is in variable 'parm' and all the positional parameters up to keyword comprise have been removed.
    Jean-Pierre.

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Thank you. I do have a question where in the second part are you checking for the Keyword so elements can be put into a string
    The 'shift' in the first part has dropped these off the parameter list.

    Try...
    Code:
    set a b c d
    echo $@
    shift
    echo $@
    You can simplify the second part if you code like this...
    Code:
    set -A theRightPartArray $@
    I believe that should work in bash.

    Damian

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    You can simplify the second part if you code like this...
    Code:
    set -A theRightPartArray $@
    Oops! As JP has already shown you!

  7. #7
    Join Date
    Jan 2004
    Posts
    4
    Thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •