Problem accessing array elements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem accessing array elements
# 1  
Old 02-26-2011
Problem accessing array elements

Hi all,
I can’t resolve an array element access issue on (Linux/pdksh) .. So I’m positing for advice.By the way - a friend demonstrated to me - same script works as expected under Solaris.

I have been working on a documentation project where many *.jpg screen shots are used in the documentation. The process that we go through - has us inserting additional images into the sequence of JPG files (where required). Eventually the sequence of images is “frozen” before they go into the next phase of the documentation process.


We need to have these images renumbered before the sequence is frozen. So for example we might start with:
Image_1

Image_34
With the additional insertions (and deletions too) – the directory structure morphs into something ugly like:
Image_1
Image_1a
Image_1b
Image_9
Image_9c
Image_9f
Image_34
Image_34h
We need to have a script process the directory structure and sort / rename these files into something like Image_0000, Image_0005, image_00010, image_0015, etc. (incremented by 5’s).



Having 5 spaces (between files) allows us to insert images into the sequence – if we should need to in a last minute emergency effort during later stages of the documentation process.


The problem:

The script is giving us problems (on linux). I can load the array in a loop – but I can NOT access the array elements outside of the loop and I am looking for advice on how to resolve this issue on the Linux box.



Please note - The same script works on Solaris as expected – array elements are accessible outside of the loop!!! (We cant use the Solaris box for this process).


Thank you for your time and input on this matter - Dave

Code:
#!/bin/ksh

touch /tmp/1.jpg /tmp/23.jpg /tmp/65.jpg /tmp/88.jpg

i=0
ls -alF /tmp/*.[jJ][pP][gG] | awk '{print $8}' | while read filein
do
   array[$i]=$filein
   echo "in loop: i=$i, Array($i)='s: ${array[$i]}"
   i=$(($i + 1))
done

echo "out of loop - 2nd element: ${array[2]}"
echo "out of loop - all elements: ${array[@]}"

rm /tmp/1.jpg /tmp/23.jpg /tmp/65.jpg /tmp/88.jpg


Last edited by Franklin52; 02-26-2011 at 05:33 PM.. Reason: Please use code tags
# 2  
Old 02-26-2011
This is a useless use of ls *:
Code:
ls -alF /tmp/*.[jJ][pP][gG] | awk '{print $8}' | while read filein

You can strip out a ls, a read, and an awk by just doing this instead:
Since you're globbing anyway, why not just do:
Code:
for filein in /tmp/*.[jJ][pP][gG]

The pipe chain was causing your array problems because, in many shells, that entire loop behind the pipes will be executed inside a separate subshell.
# 3  
Old 02-26-2011
Quote:
Originally Posted by Corona688
This is a useless use of ls *:
Code:
ls -alF /tmp/*.[jJ][pP][gG] | awk '{print $8}' | while read filein

You can strip out a ls, a read, and an awk by just doing this instead:
Since you're globbing anyway, why not just do:
Code:
for filein in /tmp/*.[jJ][pP][gG]

The pipe chain was causing your array problems because, in many shells, that entire loop behind the pipes will be executed inside a separate subshell.
isn't there a setting in ksh, similar to bash's "shopt -s nocaseglob" to enable case insensitive globbing?
# 4  
Old 02-27-2011
also pdksh is not maintained anymore.
you might like to try mksh
MirOS: mksh ? the MirBSD Korn Shell

my tip:
when you start resorting to arrays in shell scripts maybe you are using the wrong tool.
# 5  
Old 02-27-2011
thanks

Corona688:
thank you - your advice worked nicely... That web page was rather informative (bookmarked that one).

Kurumi:
RE: that entire loop behind the pipes will be executed inside a separate subshell.

yes that's what I was thinking when I saw the array data in the loop - but not external to the loop.
Bigearsbilly:
I was not aware of the status of pdksh (I'll be moving the ubuntu systems over to mksh I guess). I installed mksh on my ubuntu 11.04 (beta) machine and tested again - same error. I still would have changed / optimized the line - but wanted to see if mksh would handle the issue differently.

RE: when you start resorting to arrays in shell scripts maybe you are using the wrong tool.

Yeah - I generally avoid arrays in ksh, and was thinking about moving over to perl... But:
  • I knew I was doing something blatantly wrong - and a simple fix was probably not too far away.
  • The use of the array was the right way to handle this simple task.
  • Others in the group have agreed to use/support ksh in the environment. So using perl for this one simple task, seemed a bit excessive for what we wanted to do.
All: Thanks for your time on this matter!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

2. Shell Programming and Scripting

Random elements from array

Hi I wanted to print random elements from an array at bash shell I use the following code, but I always see first element getting printed #!/bin/bash c=1 expressions=(pink red white yellow purple) while ]; do echo "The value of RANDOM is $RANDOM" selectedexpression=${expressions}]};... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

3. Shell Programming and Scripting

Grouping array elements - possible?

I have a script which takes backup of some configuration files on my server. It does that by using an array which contains the complete path to the files to backup. It copys the files to a pre defined dir. Each "program" has it's own folder, ex. apache.conf is being copied to /predefined... (7 Replies)
Discussion started by: dnn
7 Replies

4. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

5. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

6. UNIX for Dummies Questions & Answers

Help with replacing Array elements

Hi, I have an array containing following sample information @array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07) I need to replace all duplicate entries by an underscore to get the following output@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _... (4 Replies)
Discussion started by: pawannoel
4 Replies

7. Shell Programming and Scripting

Array with String Elements

How can I get my array to understand the double-quotes I'm passing into it are to separate text strings and not part of an element? here's what I'm working with... db2 -v connect to foo db2 -x "select '\"' || stats_command || '\",' from db2law1.parallel_runstats where tabname = 'BAZ'" set... (4 Replies)
Discussion started by: djschmitt
4 Replies

8. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies
Login or Register to Ask a Question