Good day! Question: there are several variables, let's say var_1024 = "1", var_2048 = "2" etc ... for earlier the number of variables is not known. How can you refer to each variable in the script without knowing their names?
- Uh ... And what is still known? - skegg
|
2 answers
I understand, you need to construct the names of variables and then work with them.
Possible with eval
i=33 eval var_$i="AAAA" eval echo \$var_$i
The variable var_33
, a value is assigned to it, and then it is printed to echo
.
If the name of one variable is assigned to another variable, then there is another way to access its value.
var_1="AAA" n=1 nam="var_$n" echo ${!nam} eval "echo \$$nam" #то же самое
- second option ATP. - e_klimin
|
Maybe something like this:
#!/bin/bash declare -A `set | grep ^VAR | sed -e 's/^VAR_[0-9]\+/a[\0]/'` for i in ${!a[@]} do echo $i ${a[$i]} done
Variables VAR_xxxx must be exported to the script. Well, if there is no order, sort it. If the associative array is not needed, then
#!/bin/bash for i in `set | grep ^VAR` do echo $i done
or something else
- not quite that - e_klimin
- but still cool - skegg
|