If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > positional parameters

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-10-04, 16:36
J Fitch J Fitch is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
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?
Reply With Quote
  #2 (permalink)  
Old 02-10-04, 17:24
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 02-11-04, 10:06
J Fitch J Fitch is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 02-11-04, 10:22
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #5 (permalink)  
Old 02-11-04, 10:22
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
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
Reply With Quote
  #6 (permalink)  
Old 02-11-04, 10:25
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
You can simplify the second part if you code like this...
Code:
set -A theRightPartArray $@
Oops! As JP has already shown you!
Reply With Quote
  #7 (permalink)  
Old 02-11-04, 13:32
J Fitch J Fitch is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
Thank you all
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On