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 |
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.
|
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. |
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 |
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. |
All times are GMT -5. The time now is 03:39 PM. |