Array copy in ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Array copy in ksh
# 1  
Old 03-04-2010
Array copy in ksh

Hi all,
Following code in ksh is giving error:

Code:
fileLimit=5
func(){
 dir="$1"
 format="$2"
 array="$3"
 i=0
 ls -lrt $format | tail -${fileLimit} | while read f_det; do
 files[i]="${f_det},"
 ((i+=1))
 done
 eval $(echo set -A $array '"${files[@]}"')
}
 
func "." "*.pl" "a"
echo ${a[@]}


But if the line:
Code:
ls -lrt $format | tail -${fileLimit} | while read f_det; do

is changed to :

Code:
ls -lrt | while read f_det; do

it does not give any error...

any reason..?

what should be done the example to make it work..

Please help

Last edited by PRKS; 03-04-2010 at 07:25 AM.. Reason: code was not showing properly..
# 2  
Old 03-04-2010
Use code tag in your post to post the code.
# 3  
Old 03-04-2010
PRKS, are those """s part of your shell script?
# 4  
Old 03-04-2010
If we remove all """ and indenting for readability we are left with:

Code:
fileLimit=5
func ()
{
 dir="$1"
 format="$2"
 array="$3"
 i=0
 ls -lrt $format | tail -${fileLimit} | while read f_det
do
      files[i]="${f_det}",
      ((i+=1))
done
eval $(echo set -A $array '${files[@]}')
}
 
func . "*.pl" a
echo ${a[@]}

Bearing in mind that I never use arrays and cannot verify that code. There is probably a spurious comma, but the main problem is that we are expanding "*.pl" on the function call rather than within the function. Needs quotes until the point when you actually want to expand the "*". Better to quote every text variable as good practice. There is also a space character missing on the function header (which might still work in some shells).

Because "ls -l" gives more than one field and more than one line (with tail -5) the best I can suggest is to put quotes round ${f_det}. I really don't know whether this will end up in the array. ... others will.



Btw. If you have further issues, please post what you typed, what you expected to happen, and what actually happened (including any error messages).

Last edited by methyl; 03-04-2010 at 05:50 PM.. Reason: removed "just noticed", added Btw.
# 5  
Old 03-05-2010
Original script...

---------- Post updated at 06:22 AM ---------- Previous update was at 06:19 AM ----------

Code:
fileLimit=5
func(){
        dir="$1"
        format="$2"
        array="$3"
        i=0
        ls -lrt | tail -5  | while read f_det; do
                files[i]="${f_det},"
                ((i+=1))
        done
        eval $(echo set -A $array '"${files[@]}"')
}

func "." "*.pl" "a"
echo ${a[@]}


Last edited by Franklin52; 03-05-2010 at 08:42 AM.. Reason: Please use code tags!
# 6  
Old 03-05-2010
You did not say which version as ksh you were using. Assuming you are using ksh93, the following should work for you. It uses a nameref to avoid having to do an array copy.
Code:
#!/bin/ksh93

func()
{
    dir=$1
    format="$2"
    set -A $3
    nameref files=$3

    ls -lrt $format | tail -5 |
    while read f_det
    do
        files+=("${f_det},")
    done
}

func "." "*.pl" "a"
echo ${a[@]}

This gives for folowing output for me in a directory containig a number of .pl files
Code:
$ ./test.ksh
-rwxr-xr-x 1 fpm usr 427 Feb 7 2008 p6.pl, -rwxr-xr-x 1 fpm usr 136 Feb 7 2008 p7.pl, -rwxr-xr-x 1 fpm usr 215 Mar 22 20
08 p8.pl, -rwxr-xr-x 1 fpm usr 603 Mar 29 2008 p9.pl, -rwxr-xr-x 1 fpm usr 226 Mar 29 2008 p10.pl,

I am not sure why you want it in that particular format but good luck parsing it.
# 7  
Old 03-05-2010
Hi...
ksh version in "Version 11/16/88"

Its giving this error...
try: Syntax error at line 11 : `(' is not expected.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy of array by index value fails

Hello, I have a complicated situational find and replace that I wrote in bash because I didn't know how to do everything in awk. The code works but is very slow, as expected. To create my modified file, I am looping through an array that was populated earlier and making some replacements at... (6 Replies)
Discussion started by: LMHmedchem
6 Replies

2. Shell Programming and Scripting

Assigning * as value in a ksh array

I want to extract each and single character from a password string and put it in an array. I tried this : set -A password "echo $passwd | awk '{for (i=1; i<=length($1); i++) printf "%s ",substr($1,i,1)}'` It's working as long that the password string doesn't contains any * I tried a few... (5 Replies)
Discussion started by: ce9888
5 Replies

3. Shell Programming and Scripting

Array Count and Array Copy help

Hi all, I have the code as below " echo "File carried list after 1st loop "${fileStreamAssiagnInit}"" and I have the out put for the above code as below : Output : File carried list after 1st loop abcInd.csv sdgUS.csv sopSing.csv Here i want to count the number of elements in... (3 Replies)
Discussion started by: Balasankar
3 Replies

4. Shell Programming and Scripting

[Solved] KSH: Array/If Help

RedHat 5 KSH I am creating an array, and then using case to go through and count for specific words. Then the count gets stored as an expression. string='ftp rcp rsh telnet ftp ftp' set -A myarray $string FTPCOUNT="0" for command in ${myarray} do case $command in ftp) FTPCOUNT=`expr... (2 Replies)
Discussion started by: nitrobass24
2 Replies

5. UNIX and Linux Applications

Array in Ksh

Hi Guys, My code is something like this set -A A1 1 7 13 19 set -A A2 2 8 14 20 set -A A3 3 9 15 21 echo "Enter a number" read number for i in 0 2 3 4 do if }" ] then do something elif }" ] then do something elif }" ] then do something (4 Replies)
Discussion started by: jeanzibbin
4 Replies

6. Shell Programming and Scripting

Array in ksh with if-else

Hi All, My Requirement is as follows: 1. User will input Source Sytem Code as input. 2. I have source system codes as 11, 34, 56, 99, 45 etc. OS Version: SunOS 5.8 Generic_117350-62 sun4u sparc SUNW,Sun-Fire-V890 My code is like... echo 'Source System Code: \c' read varSSCode... (3 Replies)
Discussion started by: saps19
3 Replies

7. UNIX for Dummies Questions & Answers

Need help with KSH Array assignment

The following command is only intermittently successful, depends on the data I give it: set -A ImageShifts_sorted `awk '/START_SECTION IMAGE_DEFINITION/ {getline;getline;getline;getline; print $2"-"$3}' temp.ASCII | sort -u` My Error: set: -1: unknown option Finally, If I run that... (3 Replies)
Discussion started by: nerdcurious
3 Replies

8. Shell Programming and Scripting

Ksh array solution.

I was wondering if ksh supported arrays. I have a script that may work with several hosts. I'd like a means of knowing how many hosts I'm working with and an easy way to access them (as variables) in a loop. I'm assuming there's some kind of foreach in shell scripting. (1 Reply)
Discussion started by: mrwatkin
1 Replies

9. Shell Programming and Scripting

how to copy one string in ksh into another

Hi Does anybody know if there is a utility/command in ksh which would allow to copy/insert the contents of one string into certain positions of the other? for example: A=" ABCDEF " B="HHH" I need to insert contents of string "B" into string "A" from position 3 to 5, so... (3 Replies)
Discussion started by: aoussenko
3 Replies

10. Shell Programming and Scripting

using array in ksh

hi all, need help with putting names in an array, i have a few servers which i look up by doing a 'find . -name "*.pid' and the format of the output is like following : ./servername/myserver.pid i was wondering how can i iterate through and store each name in one array my code is... (1 Reply)
Discussion started by: cesarNZ
1 Replies
Login or Register to Ask a Question