Array copy in ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Array copy in ksh
# 8  
Old 03-05-2010
You are using a very old version of ksh. The script I provided will not run on that version. What operating system are you using?
# 9  
Old 03-06-2010
Hi fpmurphy,
I m using HP-UX.

All I want to do is to make a function that will store the list of files of a given directory (in an array perhaps) and return that array..

something like
Code:
listDir(){
...
...
return a; # a is an array
}

set -A arr1 $(listDir $dir1 $limit1)    #limit is the tail limit of ls -lrt
set -A arr2 $(listDir $dir2 $limit2)
set -A arr3 $(listDir $dir3 $limit3)

# 10  
Old 03-06-2010
The return value of a function is a number, not a string.

One approach is to create your function (customise as you require) and pass the new array name to the function.

i.e.

Code:
$ cat ArrTest
GetDir() {
  cd $2
  ls -ltr | tail -$3 | while read FILE; do
    eval $1[\${#$1[@]}]="\$FILE"
  done
  cd - > /dev/null
}

# GetDir $1 is new array name, $2 is directory, $3 is the value for tail
GetDir A /tmp 5
echo A[0] is ${A[0]}
echo A[1] is ${A[1]}
echo
GetDir B /Users/scott 3
echo B[0] is ${B[0]}
echo B[1] is ${B[1]}


$ ./ArrTest
A[0] is drwx------ 3 scott wheel 102 5 Mar 20:49 launch-7Y8JI8
A[1] is drwx------ 3 scott wheel 102 5 Mar 20:49 launch-3RaSBo

B[0] is drwx------+ 79 scott staff 2686 26 Feb 20:54 Downloads
B[1] is drwxr-xr-x 34 scott staff 1156 5 Mar 00:33 scripts


Last edited by Scott; 03-06-2010 at 09:19 AM.. Reason: Added tail - updated output
# 11  
Old 03-08-2010
The ksh in HP-UX 11.11 responds to "what" with vintage 11/16/88 (i.e. ksh88).
Not ksh93
Hope this helps.
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