LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2006, 06:37 PM   #1
manojg
Member
 
Registered: May 2004
Posts: 78

Rep: Reputation: 15
end of file in bash shell


Hi,

Could you please tell me how to check the "end of file" by using bash script and c script?

Thanks a lot.
Manojg

Last edited by manojg; 03-08-2006 at 06:38 PM.
 
Old 03-08-2006, 07:27 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
For your bash script, how are you processing the file? Tools like cat, grep etc. already know when they're at the end of the file.
 
Old 03-08-2006, 08:09 PM   #3
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
Actually, I used:

================
exec 6<file.dat # open the file
read -u 6 dta # read the one line of the file
================

If I loop over the "read -u 6 dta", it will end up at the end of the file. It is fine. But I want to put these quantities in a 1-d array. So, I have give the index for the array. To do this I have to make a loop (over a large number because the program does not know the number of entries in file) and this loop should terminate at the end of the file.
Some thing like this:

======================
declare -a nentry # declare an 1-d array "nentry"
entry=0 # initialize the number of entry
exec 6<file.dat # open the data file

while [ $entry -le 500 ]; do # loop over entries
read -u 6 dta # read one line
nentry[$temp]=$dta # put the quantity in array
entry=$(expr $entry + 1) # increase the index by one
done
======================

The loop will be executed 500 time even if there are only 10 entries. So, I want to terminate the loop after 10th execution of the loop.
One way I can do this is by putting a number (say 22222) at the end of the file and checking for that number by using "if" condition like:

=================
if [$dta -eq 22222 ]; then
break
fi

=================

Then the loop will be:

==================
while [ $entry -le 500 ]; do
read -u 6 dta
if [$dta -eq 22222 ] break
nentry[$temp]=$dta
entry=$(expr $entry + 1)
done

===================

But this does not work because in the data file there are number as well as character. So, the if condition shows problem when it encounters characters.
 
Old 03-08-2006, 11:34 PM   #4
Valdis Grinbergs
Member
 
Registered: Dec 2005
Distribution: Debian
Posts: 30

Rep: Reputation: 25
Manojg,
Like Gilead said, tools like cat handle the end of file for you.
Here is a example of how you can use it in a bash script to do what you describe:

entry=0
for item in $( cat file.dat ); do
entry_set[$entry]=$item
entry=$( expr $entry + 1 )
done

You can see the contents of the array with:

counter=0
while [ $counter -lt $entry ]; do
echo Number stored in array block $counter is ${entry_set[$counter]}
counter=$( expr $counter + 1 )
done

However, if you can, I recommend you use Python instead of a bash script. Although the difference is small in this case, Python can do so much more than a bash script it is often a better tool.
Here is the Python code to write to an "array." I actually used a Python list instead. A list is a kind of variable array.

file_handle = open("file.dat")
entry_set = []
for item in file_handle:
entry_set.append(item)

Viewing the "array" in Python would be:

for i, output in enumerate(entry_set):
print "Number stored in list block", i, "is", output,

I hope this helps

Last edited by Valdis Grinbergs; 03-08-2006 at 11:36 PM.
 
Old 03-09-2006, 11:11 AM   #5
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
Thanks a lot everybody for your help and information. I will also use Python.
Actually, I was making a dialog box using Xdialog which runs with shell script. Fortuantly, there is Xdialog for Python also.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 10:41 AM
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 03:44 AM
how do i chage the color of a file or a directory in a bash shell Paxmaster Linux - Software 3 10-18-2004 12:21 AM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 04:40 AM
systemwide bash login shell cleanup file markus1982 Linux - General 2 05-10-2003 09:35 AM


All times are GMT -5. The time now is 11:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration