WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: ash/script - Ho to create an 'indexed' variable  (Read 11288 times)

Offline deetee

  • Newbie
  • *
  • Posts: 34
ash/script - Ho to create an 'indexed' variable
« 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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11515
Re: ash/script - Ho to create an 'indexed' variable
« Reply #1 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.

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: ash/script - Ho to create an 'indexed' variable
« Reply #2 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

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: ash/script - Ho to create an 'indexed' variable
« Reply #3 on: June 05, 2013, 03:08:03 PM »
« Last Edit: June 05, 2013, 03:12:37 PM by tinypoodle »
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: ash/script - Ho to create an 'indexed' variable
« Reply #4 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
« Last Edit: June 05, 2013, 03:19:19 PM by deetee »

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: ash/script - Ho to create an 'indexed' variable
« Reply #5 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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11515

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: ash/script - Ho to create an 'indexed' variable
« Reply #7 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

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: ash/script - Ho to create an 'indexed' variable
« Reply #8 on: June 06, 2013, 01:33:39 PM »
Code: [Select]
i=3
export string$i="asdr \ jkl"
eval echo \$string$i|tr -d '\'
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: ash/script - Ho to create an 'indexed' variable
« Reply #9 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