How to append to array within conditional block in ksh/korn shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append to array within conditional block in ksh/korn shell?
# 1  
Old 05-16-2013
How to append to array within conditional block in ksh/korn shell?

Hi,
I have one array created and some values are there in ksh. I want to append some other values to it based on some condition in if statement.
Code:
#!/bin/ksh
echo "---------------------------------------------------"
        set -A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}' | uniq) #hosts ips
        set +A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}') 


        if [ "$os" = "solaris" ]
        then
                set +A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}')
        else
               #some other statement
        fi

        COUNT=0
        for str in ${ipaddr_arr[*]}
        do
                echo "$str"
                COUNT=$((COUNT+1))
        done

However after the if condition is executed as true, the array does not contain previous values. The statement within if overwrites the array values, without appending the values to it. As in ksh, +A is for appending values to array. However here it is not behaving properly.

I tried with only giving this in if block, still it gives error in ksh-
Code:
+A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}')

Anyone have any clues..
thanks in advance for your replies.

Last edited by zaxxon; 05-16-2013 at 11:58 AM.. Reason: more code tags
# 2  
Old 05-16-2013
I believe both commands( set -A / set +A ) start at element 0:
+A name assigns the parameter list to the elements of name, starting at name[0].
-A name unsets name, then assigns the parameter list to the elements of name starting at name[0].
# 3  
Old 05-17-2013
No, -A option is not working.
What I know +A is to append to array, -A is for creating the array clearing off any previous values.
Any other clues..?
thanks for the reply.

---------- Post updated at 06:25 PM ---------- Previous update was at 11:37 AM ----------

I have found the answer after putting little effort and googling. You can append to the original array with below code snippet-
Code:
        set -A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}' | uniq) #hosts ips
        set -A other_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}') 


        if [ "$os" = "solaris" ]
        then
                set -A third_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}')
        else
               #some other statement
        fi
   
       set -A ipaddr_arr ${ipaddr_arr[*]} ${other_arr[*]} ${third_arr[*]}
       #the above code snippet will append all other array values to the first ipaddr_arr

thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

'*' vs. '@' in Korn Shell Array Variables

In order to use the shellcurses functions described at: Shell Curses function library I am learning about ksh, which has arrays. My trusty Kochan & Wood book says that for any Korn Shell array AR : ${AR } expands to all the defined array elements, and ${#AR } expands to the number... (3 Replies)
Discussion started by: Clovis_Sangrail
3 Replies

2. Shell Programming and Scripting

Korn Shell Array maximum value less than other value

I have a text file with several key words that I am trying to isolate. I have grepped for the unknowns in the text file, but each unknown has a corresponding location. I have created an array that holds all the unknowns and another array that holds all of the locations and compares them based on... (12 Replies)
Discussion started by: ther2000
12 Replies

3. Shell Programming and Scripting

Array in Ksh Shell script

hi team, i have a file, which contains only variable and its value param.ksh --------- export A=123 export B=345 export C=567 export D=OPLI export E=OL89PO From shell script, i am invoking this file and use the value of this variable. Now there are 5 variable in above file. Before i... (1 Reply)
Discussion started by: ace_friends22
1 Replies

4. Shell Programming and Scripting

Generate array name at run time Korn shell

Hi, I am trying to define number of array based on constant derived during execution phase of a script. Here is what i am trying.. #First Part, Get LUN input from User lun_count=4 count=0 set -A my_lun while : do while ]; do read L?"Enter Lun "$count" Number:" ... (2 Replies)
Discussion started by: harris2107
2 Replies

5. Shell Programming and Scripting

need help with korn shell array

I have a korn shell script that reads a file with just one column in the file. If the file has more than 5 entries it is split using split -5. This means that is we have 15 entries I will end up with 3 files with 5 entries/lines in each and if I have 23 entries I will end up with 5 files with the... (2 Replies)
Discussion started by: kieranfoley
2 Replies

6. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

7. Shell Programming and Scripting

I can't decalar an array in my korn shell script

I have a script that contains #!/usr/bin/ksh set -A X 'hallo' 'world' echo ${X} echo ${X} when I execute it I get an errror message sh ./test.ksh ./test.ksh: -A: bad option(s) but if I do this at the command prompt I am able to create it set -A myarray '1' '2' '3' echo ${myarray}... (4 Replies)
Discussion started by: zilla30066
4 Replies

8. Shell Programming and Scripting

Unix Korn Shell Array Issue (SunOS)

Hello, I'm currently messing around with arrays for the first time in scripting (Unix Korn Shell). All I'm trying to do right now before I make things complicated is read through and print out to screen whether the read file is or is not a directory. Here is my directory: ls -l total... (5 Replies)
Discussion started by: Janus
5 Replies

9. Shell Programming and Scripting

korn shell array?

I read it is possible to provide values for an array with the -A option to the read statement; however, I have not been able to get this to work. When I execute a script with the -A option to the read statement, the shell complains that it is an illegal option. If this works, can someone provide... (5 Replies)
Discussion started by: cstovall
5 Replies

10. UNIX for Advanced & Expert Users

Oracle To Korn Shell Array

I'm attempting to populate an array in ksh using the following command: set -A $(SELECT_INVOICE | sed '/^$/d') SELECT_INVOICE is a function that executes the SQL query. Problem: Some of the invoice numbers have alpha characters with spaces(example: OVEN MICRO). The Korn shell is treating... (1 Reply)
Discussion started by: kdst
1 Replies
Login or Register to Ask a Question