I'm sure it is relatively simple, I just don't know how to do it.
#!/usr/bin/ksh
set `iostat`
myvar=6
I want to something like echo ${$myvar}
which i want interpreted as ${$myvar}
-> ${6}
-> value
I'm sure it is relatively simple, I just don't know how to do it.
I want to something like |
|||||
|
You can do this sort of thing with
The trick is to double-quote the string you feed to I got "%user" for the output, but I tried it on a multi-processor RHEL machine. |
|||||
|
Another use of arraysHaven't used either ksh or any variant for some time, so I'm not sure if ksh(or bash) has a similar capability. My primary shell is zsh. I use arrays when handling output from commands like iostat because they produce multiple lines, and not all lines are the same format/length.
The above also bypasses the use of positional parameters. Now, if you want to generate, say, an array of devices:
I find smaller chunks much easier to handle. You may or may not need to use indirect variable reference, depending on your code. Knowing how it works is still a good thing to know. I use it myself. |
|||
|
Indirect variable referenceModern advanced shells have a method to reference the value of a variable whose name is stored in another variable. Unfortunately the method differs between ksh, bash and zsh. In mksh ≥R39b, you can make
This doesn't work in ATT ksh93 because it doesn't support namerefs to positional parameters. In the case where you have a variable containing a variable name, you can use this method.
In bash ≥2.0, you can write
In zsh, you can write
In older shells, including ksh88 and pdksh, your only recourse when you have a variable containing another variable name and want to use the value of this variable
Using an arrayThis is the best method here: it's simpler and more portable. For your use case, in any shell with arrays (all ksh variants, bash ≥2.0, zsh), you can assign to an array variable and take the element you wish. Beware that ksh and bash arrays start numbering at 0, but zsh starts at 1 unless you issue
If you want to copy the positional parameters to an array variable
In ksh93, mksh ≥R39b, bash ≥2.0 and zsh, you can use the array assignment syntax:
|
|||||
|
As indicated by Gilles (who provided the
Added the |
|||||||||
|