Korn shell arrays

Korn shell supports arrays. You can have about 4000 elements in a korn shell array, and the performance is not great, but they're quite handy for walking through sets of variables (like paths).
The syntax is:
set -A <array> [values...]
This will set the array named to the values passed, clearing the variable before setting. This is fine, but what if we're simply appending to an array? Lots of folks want to do that. In this case what you use is:
set +A <array> [values...]
All very fine and well, but how do we reference the values in the array? You use the variable[<index>] syntax.
array[<index>]=<value>
What about getting all the values from the array? simply use either @ or \* as the index.
Then there's dereferencing it. Dereferencing the entry involves using ${array[index]}, otherwise the $ would evaluate up to the left bracket, and not evaluate the content of the array, such is the grammar of ksh.
Putting this all together into something useful (like a gcc wrapper for gcc specific options)
set -A arguments -- "$@"
typeset -i index=0
while [[ -n ${arguments[$index]} ]]; do
	case ${arguments[$index]} in
		-W|-Wall)
			arguments[$index]=-v;;
		-Werror)
			arguments[$index]=-errwarn=%all;;
		-O?)
			arguments[$index]=-xO${arguments[$index]##-O};;
		
	esac
	index=$((index + 1))
done

cc -xCC "${arguments[@]}"
Comments:

Post a Comment:
Comments are closed for this entry.
About

petesh

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today