0

I have two arrays: options and args, and I want to display the contents of arrays. For that I wish to write a general function which will take the name of array and display its contents.

eg.

set -A options val1 val2 val3
set -A args var1 var2 var3

What I am doing now to display the contents:

dispArr() {
    i=0
    while [ "$i" -lt "${#options[*]}" ] #line1
    do
        echo ${options[$i]} #line2
        ((i=i+1))
    done
}

currently I have two different functions, one for array "options" and other for array "args"

I want to substitute array names ("options" in above code) in line1 and line2 with variables, so that I can make a call like following to print the contents of any array :

dispArr options #print the contents of array "options"
dispArr args #print the contents of array "args"

I tried providing $1 in place of array names but it didn't work. I also tried different quotes but it didn't work too.

I am new to unix and shell scripting, so any advice would be appreciated. Thanks in advance.

2
  • What version of ksh? ksh88 or ksh93? Do ksh --version -- empty output, then it's ksh88 Commented Aug 12, 2015 at 12:53
  • @glennjackman Following is the output of ksh --version: version sh (AT&T Research) 93u 2011-02-08 Commented Aug 12, 2015 at 13:20

1 Answer 1

2

ksh93

dispArr() { nameref arr=$1; printf "%s\n" "${arr[@]}"; }

ksh88

dispArr() { eval printf "%s\\\\n" "\${${1}[@]}"; } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.