Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-08-2006, 05:37 PM   #1
Registered: May 2004
Posts: 78

Rep: Reputation: 15
end of file in bash shell


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

Thanks a lot.

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

Rep: Reputation: 166Reputation: 166
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, 07:09 PM   #3
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

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


Then the loop will be:

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


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, 10:34 PM   #4
Valdis Grinbergs
Registered: Dec 2005
Distribution: Debian
Posts: 30

Rep: Reputation: 25
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:

for item in $( cat file.dat ); do
entry=$( expr $entry + 1 )

You can see the contents of the array with:

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

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:

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 10:36 PM.
Old 03-09-2006, 10:11 AM   #5
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.


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 09:41 AM
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM
how do i chage the color of a file or a directory in a bash shell Paxmaster Linux - Software 3 10-17-2004 11:21 PM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM
systemwide bash login shell cleanup file markus1982 Linux - General 2 05-10-2003 08:35 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:31 AM.

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