Quantcast
Channel: What are file descriptors, explained in simple terms? - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by sumitsinghdeode for What are file descriptors, explained in simple terms?

$
0
0

Addition to above all simplified responses.
If you are working with files in bash script, it's better to use file descriptor.
For example:-
You want to read and write from/to the file "test.txt".
Use the file descriptor as show below

FILE=$1 # give the name of file in the command line
exec 5<>$FILE # '5' here act as the file descriptor
# Reading from the file line by line using file descriptor
while read LINE; do
    echo "$LINE"
done <&5

# Writing to the file using descriptor
echo "Adding the date: `date`" >&5 
exec 5<&- # Closing a file descriptor

Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>