Understanding reading and writing to a file

In the earlier examples, we opened the file either for reading or writing. Now we will see how to open the file for reading and writing purposes:

    exec fd<> fileName

If the file descriptor number is not specified, then 0 will be used in its place. The file will be created if it does not exist. This procedure is useful for updating files.

Let's understand the following script.

Write the shell script file_03.sh as follows:

#!/bin/bash file_name="sample_out.txt" # We are assing fd number 3 to file. # We will be doing read and write operations on file exec 3<> $file_name # Writing to file echo """ Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment. - Buddha ...

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.