Tiny Core Linux
General TC => Programming & Scripting - Unofficial => Topic started by: deetee on June 05, 2013, 09:23:24 AM
-
Hello all!
Unfortunately ash doesn't support arrays.
Is there a possibility to 'index' a variable anyway?
I tried:
i=3
string$i=asdfghjkl
eval echo \$string$i
Instead of the expected output "asdfghjkl" I got the error message "string3=asdfghjkl: not found".
How do I assign it the right way?
TIA
deetee
-
Hi deetee
There are some ways to kind of fake it, Google linux ash index array. I believe awk has support for arrays.
However, if you plan on making extensive use of arrays, you might be better of using bash.
-
Hi Rich!
Thanks for guiding me again.
I still tried to search the web but because of dominating bash-info it is like looking for a needle in a haystack.
I got very far in echoing a pseudo-indexed variable, but I didn't find a solution for assigning a value to such a variable (string$i=asdfghjkl).
I'm sure that there is an solution with ash - I'll try to search deeper.
Thanks for the hint to awk.
deetee
-
Perhaps look into using 'export'?
http://pubs.opengroup.org/onlinepubs/009604499/utilities/export.html
-
Hello Rich!
On stackoverflow.com I just found a solution.
Again it was "eval" which made the assignment possible.
i=3
eval string$i=asdfghjkl
eval echo \$string$i
Thanks again for guiding me.
@ tinypoodle:
Thanks for your hint using "export" - I gave it a try and it seems to work too (export string$i=asdfghjkl).
deetee
-
Hi!
Please allow me an additional question regarding this topic.
i=3
export string$i="asdf jkl" # string contains 2 spaces
eval echo \$string$i
This code trims the string (with two spaces) to a string with one space.
How do I avoid that echo trims spaces?
Setting double quotes, like it's recommended in script forums, doesn't work properly with such "pseudo indexed" variables.
TIA
deetee
-
Hi deetee
Here's one possible answer:
http://logbuffer.wordpress.com/2010/09/23/bash-scripting-preserve-whitespaces-in-variables/
-
Hello Rich!
Thank you.
Maybe I should improve my web-querying-strategy? :-(
But I swear I tried intensively to find a solution - but maybe it lacks on my poor basic shell experience.
This code works:
i=3
export string$i="asdf jkl"
IFS='%'
eval echo \$string$i
unset IFS
Regards
deetee
-
i=3
export string$i="asdr \ jkl"
eval echo \$string$i|tr -d '\'
-
Hello tinypoodle!
Your solution is very tricky (didn't see the translate characters command before) - that's high level script artistry.
As my strings consist of many spaces - this solution is too extensive for me. But I'm sure I can use this code somewhere else soon.
Thanks to learn from you.
deetee