The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
xlC compilation error when dealing with operator overloading luop0812 AIX 1 04-09-2008 04:09 PM
perl: When dealing with files that do not exist joeyg Shell Programming and Scripting 2 02-20-2008 05:09 PM
tsch script problem (dealing with sed) csnewbie84 Shell Programming and Scripting 10 05-02-2007 10:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-27-2007
jojan jojan is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 12
Help in dealing with arra

I am readinga file lin by line and based craeting a arry of unique elemenst from the second column of the line. However when i coem out of the while loop my array becomes empty , can eny one tell me what I would be doing wrong
#!/bin/bash

logfile="./mylog.dat"
begin=100
end="$(( $begin + 1000 ))!d"
index=0
Isthere=0
ENGINES=""

sed "$begin, $end" $logfile | while read line
do
i=`echo $line | awk -F "," '{print $2}'`
Isthere=0
for item in "${ENGINES[@]}" ; do
if [ "$i" = "$item" ] ; then
echo "Aleady there "
Isthere=1
break
fi
done
if [ "$Isthere" -eq 0 ] ; then
echo "adding $Isthere"
ENGINES[$index]=`expr $i`
index=$(( $index + 1))
fi
# echo $line
done
echo $index ${#ENGINES[@]} # howe ever at this point array is empty
for i in "${ENGINES[@]}" ; do
echo $i
done
  #2 (permalink)  
Old 08-28-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
Put a few lines of data in a file called test.file. Try this loop style:
cat test.file | while read line
and the array will be empty after the loop ends. Then try:
exec < test.file
while read line
and the array will have data after the loop ends. The while loop is being placed in a subshell if it is in a pipeline. ksh will not do that but other shells do.
  #3 (permalink)  
Old 08-28-2007
jojan jojan is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 12
You mean I ahve to change the below line
sed "$begin, $end" $logfile | while read line

to
exec < test.file
while read line

But I do not want to read file fully , but only from begin to end.
  #4 (permalink)  
Old 08-28-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
Change your technique to get rid of the pipeline, change your shell to ksh, or live with the empty array. These are your options. Sorry, but you can't keep the pipeline, bash, and the array contents. One has to go. Here is a simple script to illustrate the problem...
Code:
$ cat script2
#! /usr/local/bin/bash

echo "cat
dog
mouse
rabbit
lion
wolf
dog
bat
lion
hamster
rabbit
elephant
elephant
whale
cricket" > list.txt

index=0
cat list.txt | while read item ; do
                array1[index]=$item
                ((index=index+1))
done
echo array1: ${array1[@]}

exec < list.txt
index=0
while read item ; do
        array2[index]=$item
        ((index=index+1))
done
echo array2: ${array2[@]}
exit 0
$ ./script2
array1:
array2: cat dog mouse rabbit lion wolf dog bat lion hamster rabbit elephant elephant whale cricket
$
ksh is the only shell I know that will populate both arrays. This is one of the reasons that I strongly prefer ksh to other shells. (The other is ksh co-processes.) So my suggestion: switch to ksh.
  #5 (permalink)  
Old 08-28-2007
jojan jojan is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 12
Fantastic , that was new to be , I thought of getting rid of the the "disk write" happening while out putting it in to a file.

sed -n "$begin, $end" $logfile >engines
exec < engines
while read line
do
# array insert
done

Thankyou very much , I am so used t bash such that , ksh is not easy for me to use now. It does not ahve the "auto completion when we type something and the the <tab> key stroke
  #6 (permalink)  
Old 08-28-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
You can use bash as your interactive shell and still write scripts in ksh.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0