Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: deetee on June 05, 2013, 09:23:24 AM

Title: ash/script - Ho to create an 'indexed' variable
Post 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:
Code: (ash) [Select]
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
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: Rich on June 05, 2013, 10:08:00 AM
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.
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: deetee on June 05, 2013, 01:35:43 PM
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
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: tinypoodle on June 05, 2013, 03:08:03 PM
Perhaps look into using 'export'?

http://pubs.opengroup.org/onlinepubs/009604499/utilities/export.html
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: deetee on June 05, 2013, 03:12:55 PM
Hello Rich!

On stackoverflow.com I just found a solution.
Again it was "eval" which made the assignment possible.
Code: (ash) [Select]
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
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: deetee on June 06, 2013, 09:34:11 AM
Hi!

Please allow me an additional question regarding this topic.

Code: (ash) [Select]
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
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: Rich on June 06, 2013, 09:44:05 AM
Hi deetee
Here's one possible answer:
http://logbuffer.wordpress.com/2010/09/23/bash-scripting-preserve-whitespaces-in-variables/
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: deetee on June 06, 2013, 12:12:29 PM
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:
Code: (ash) [Select]
i=3
export string$i="asdf  jkl"
IFS='%'
eval echo \$string$i
unset IFS

Regards
deetee
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: tinypoodle on June 06, 2013, 01:33:39 PM
Code: [Select]
i=3
export string$i="asdr \ jkl"
eval echo \$string$i|tr -d '\'
Title: Re: ash/script - Ho to create an 'indexed' variable
Post by: deetee on June 07, 2013, 05:34:34 AM
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