Because my actual script has more arrays to loop through and I'll be adding more as time goes on, I came up with an 'all' array that has the array names in it. Then I attempted to loop through each array as such:
But this time I'm only getting items at index 0, as per the output below:
I've been struggling with this one for a while now, and I'm not entirely sure what I'm trying to accomplish is possible with Bash. Any help is appreciated!
I don't know if you can do what you're trying to do with bash, but you can do it with a 1993 or later version of ksh by using a name reference variable. You can't do it quite the way you were trying to do it because you can't currently create arrays of nameref variables, but the following seems to produce results similar to what I think you were trying to do:
which produces the output:
These 2 Users Gave Thanks to Don Cragun For This Post:
Then I attempted to loop through each array as such:
But this time I'm only getting items at index 0, as per the output below:
The underlying problem is: arrays in bash (and ksh88 as well) are ONE-dimensional. Therefore, you can create a variable holding a one-dimensional array, but you can't put other array variables as elements into this array.
You could use the following workaround, but i strongly suggest you don't. Stretching the limits of what can be done is fun and helps learning the trade, but you shouldn't put circus tricks into production code. So, with this (rather big) grain of salt, here it goes:
Variables are evaluated always in the same step and all at the same time, which is why you cannot do things like this:
This would rely on "${selector}" to be evaluated first and only then the resulting "${xfoo}" to be evaluated again. But, as i said, this is not the case and therefore this will fail.
There is one remedy for that, though: the keyword eval. eval starts the evaluation process again and this way you get (among other things) a second evaluation phase for your variables:
The same way you can create sort-of two-dimensional arrays by using this mechanism:
But again: avoid eval like the plague and if you have to use it this is usually indicative that you better search for an alternative. As a show-off of skill, though, it is pretty cool. No?
Don: It looks like I could accomplish what I want with ksh, except the output would need to be grouped by index as opposed to array names:
Quote:
You can't do it quite the way you were trying to do it because you can't currently create arrays of nameref variables
Quote:
The underlying problem is: arrays in bash (and ksh88 as well) are ONE-dimensional. Therefore, you can create a variable holding a one-dimensional array, but you can't put other array variables as elements into this array.
I had a feeling I'd get that kind of answer but was still hoping for a syntax fix
Quote:
This would rely on "${selector}" to be evaluated first and only then the resulting "${xfoo}" to be evaluated again. But, as i said, this is not the case and therefore this will fail.
Yes that's exactly what I wanted, evaluate "${selector}" first then "${xfoo}" :S
Because my script is pretty much done and I've never written ksh before, I guess I'll just stick to adding array names manually as I go. At least this implementation works in bash.
Thanks!
---------- Post updated at 12:11 PM ---------- Previous update was at 12:09 PM ----------
Quote:
As a show-off of skill, though, it is pretty cool. No?
Hi RudiC,
Thanks for letting me know bash now recognizes typeset -n. (It doesn't in the bash I have on macOS Sierra 10.12.6 which includes GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16). And, apparently it doesn't with the bash on whatever OS Kingzy is using.)
Hi Kingzy,
It is always a good idea to tell us what operating system you're using (in addition to the shell). If we know what OS and shells you have available, we're less likely to offer suggestions that won't work in your environment. Assuming that you have access to a 1993 or later version of the Korn shell, the following should work...
I had hoped that with the example code I had provided, you would have been able to reconfigure the loops in my example to get whatever output was desired. For the output format requested in post #4, the following should suffice:
which just switches the inner and outer loops in the code I suggested before, changes the names of the arrays used as looping values, and adds code to add an empty line between groups. The above produces the output requested in post #4.
Hi,
I have to write a shell script in which I have to substitute a variable within a variable. For example,
var1=aaa
var2=file.$var1.txt
The output should be,
echo $var2
file.aaa.txt
Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
For example I have variable like below
echo $OUTPUT
/some/path/`uname -n`
when I try to use the variable OUTPUT like below
cd $OUTPUT or cd ${OUTPUT}
I am getting bad substituion error message
$ cd $OUTPUT
ksh: cd: bad substitution
$ cd ${OUTPUT}
ksh: cd: bad substitution
... (1 Reply)
Hi ,
I have a variable as follows,
Temp=`cat ABC.txt | cut -c5-`
This will yeild a part of the date. say , 200912.
I would like to substitute this variable's value in a filename.
eg: File200912F.zip
when i say File$TempF.zip , it is not substituting.
Any help ?
Thanks in... (2 Replies)
Hi all.
I'm trying to finish a bash script with the following elements:
ARRAY="blah $ITEM blah blah"
ARRAY="blah blah $ITEM blah bluh"
#ARRAY="...."
# ...the ARRAY elements represent a variable but defined
# syntax and they're all hard-coded in the script.
#(...)
ITEMS='1.0 2.3... (2 Replies)
file1.ksh
#!/bin/ksh
test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line
a=${1}_create
echo $a # i need the output as "I am a man"
./file1.ksh test5 # i run the script like this
any suggessions guys... (1 Reply)
Hi,
That might be pretty simple.
How can I generate a variable name and get their value ?
Thanks a lot.
Something like:
>CUSTOMER_NF=26
> object=CUSTOMER
> echo ${object}_NF
CUSTOMER_NF
> echo ${${object}_NF}
ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Hello,
i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem.
my var1 is a string constructed like this:
filename1 filerev1 filepath1
my var2 is another string constructed like this:
filename2 filerev2 filepath2
when i do... (2 Replies)
I have run into a wall with my iptables firewall scripting.
I am blocking all of the private side IP addresses on the
WAN interface on systems running NAT. However, if the
system is not running NAT and needs to allow access to
the local LAN on the WAN interface, I need to block all
but one of... (2 Replies)
Hi everyone,
I have a simple question to ask :
In a script that I'm writting, I need to create variables on-the-fly.
For instance, for every iterartion of the following loop a var_X variable should be generated :
#!/bin/ksh
a="1 2 3"
for i in $a
do
var_${i}=$i
echo "${var_$i}"
done
... (1 Reply)
Hey All,
I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)