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
reading more than one variable into a for loop starsky UNIX for Dummies Questions & Answers 3 10-24-2008 01:01 PM
reading from 2 files through while loop windows Shell Programming and Scripting 2 08-22-2008 11:17 AM
Reading from while loop into an array ssuresh1999 UNIX for Dummies Questions & Answers 2 08-12-2008 12:53 PM
Reading from a specific line in a loop kaushikraman Shell Programming and Scripting 3 07-24-2008 06:49 AM
reading from within the loop --std i/p problem mobydick Shell Programming and Scripting 2 07-11-2008 09:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-09-2008
npatwardhan npatwardhan is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 135
while loop not reading last line

hi all,
i have a while loop which i am using to read lines into an array:

Code:
k=0
exec 10<file
while read LINE <&10; do
    ARRAY[$k]=$LINE
    ((k++))
done
exec 10>&-
echo ${ARRAY[@]}
for some reason when i display the array it is not showing the last row in the file.
any help appreciated.

Last edited by Franklin52; 12-09-2008 at 10:12 AM.. Reason: adding correct code tags
  #2 (permalink)  
Old 12-09-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Quote:
Originally Posted by npatwardhan View Post
for some reason when i display the array it is not showing the last row in the file.
I wonder why you even get so far. Furthermore, if you have a shell programming problem, it would be appreciated when you could state the shell you are using. It makes reconstructing your environment a wee bit easier.

The first problem is this line:

Code:
exec 10<file
The shell supports only I/O-buffers 1-9 as far as i know. I changed that to I/O-channel 4 in my verson of your code.

The second problem is this:

Code:
((k++))
This might work in bash, it definitely will not work in ksh, because the "((" has to be surrounded by blanks. Here is my version of your script, which works as expected (at least here):

Code:
k=0
exec 4<file
while read LINE <&4; do
     ARRAY[$k]=$LINE
     (( k+=1 ))
done
exec 4<&-
echo ${ARRAY[@]}
I hope this helps.

bakunin
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

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 04:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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