darkphase / klish

Tool for Implementing Custom Shells

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array variables define procedure and access in "test" and "ACTION"

GoogleCodeExporter opened this issue · comments

Is there any way to define Array shell variables like as follows:
myvar=(1 2 3 4 5)

To access:
echo ${myvar[0]}    # it will print 1
echo ${myvar[1]}    # it will print 2
echo ${myvar[2]}    # it will print 3
.
.
echo ${myvar[@]}    # it will print 1 2 3 4 5
echo ${myvar[*]}    # it will print 1 2 3 4 5

Original issue reported on code.google.com by panwar.rahul@gmail.com on 16 Oct 2013 at 10:10

You could define it as a string with a pattern:
pattern: \((\d )+\d\)

You would have to parse the string on your end..(using awk?)

Original comment by corent...@gmail.com on 16 Oct 2013 at 8:39

Would you please explain it with a small example. How can i write it in xml 
file and use it in "test" and "ACTION".

Original comment by panwar.rahul@gmail.com on 17 Oct 2013 at 4:58

In your XML:
<PTYPE name="DIGIT_ARRAY"
           pattern="\((\d )+\d\)"
              help="Array of digits"/>


Then for param in your command:
<PARAM name="my_param"
              help="my param Array of digits"
              ptype="DIGIT_ARRAY"/>

I don't understand what you mean by use it in test.. Can you clarify that point?

In action:
<ACTION shebang="usr/env/python">
var = ${my_param}
var = var[1:-1] #remove first and last parenthesis
var_array = var.split()
print(var_array)
</ACTION>

Please note that I haven't tested this code (I don't have Klish environment set 
up here!) I also used python as language since it is obvious how to do it in 
that language. There are solutions for bash as well (example: 
http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter
-in-bash)

Original comment by corent...@gmail.com on 19 Oct 2013 at 12:46

Original comment by serj.kalichev@gmail.com on 6 Feb 2014 at 10:17

  • Changed state: Done