faster bash operations on files with File Descriptors.

2011-06-11 361 words 2 mins read

I was writing a bash script that would do some operations and read and write to file. Seems that that was pretty simple with

  <td>
    <div class="text codecolorer">
      while read line<br /> <br /> do<br /> <br /> done<file
    </div>
  </td>
</tr>
1
2
3
4
5

and then use redirection operations like “>” and “»” to write to file. Done with the script pretty fast. So far so good, when I went for real life tests, no one was interested in using it, why? Simple, it was simply taking too long. The file was reading about 10K lines and writing about 50 lines and was taking about more than 10 minutes.

So, I sat down to debug what can increase the performance of the script and one change made the difference. The script was taking a lot of time in opening and closing the file. Pretty evident, isn’t it!!!

When using “>” or “»”, each operation would require bash to open the file, write to it and close it. Un-necessarily we would be doing a open and close for each write operation. Pathetic and useless waste of CPU power and time. How to avoid this?

Open a file and get the file descriptor. Keep writing to the file descriptor and close the descriptor after you are done with the file operations.

  <td>
    <div class="text codecolorer">
      exec 3> File<br /> <br /> echo "" >&3<br /> <br /> exec 3>&-
    </div>
  </td>
</tr>
1
2
3
4
5

In the above commands,

  <td>
    <div class="text codecolorer">
      &nbsp;exec 3> File
    </div>
  </td>
</tr>
1

will open the file descriptor FD 3

Note that when you are working with FD, you don’t need  “>>” as the echo command will put the statements in the current position of file. So, if you want to append to the file use

  <td>
    <div class="text codecolorer">
      exec 3>> File
    </div>
  </td>
</tr>
1

Then you can write to the FD with redirection and finally close the descriptor with

  <td>
    <div class="text codecolorer">
      &nbsp;exec 3>&-
    </div>
  </td>
</tr>
1

HTH

Enhanced by Zemanta


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it