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:
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
Last edited by Franklin52; 02-26-2011 at 05:33 PM..
Reason: Please use code tags
This is a useless use of ls *:
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:
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.
This is a useless use of ls *:
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:
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?
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)