Displaying the file descriptor information from the /proc folder

We will write the script to display the actual file descriptors associated with the file.

Let's write the file_06.sh script, shown as follows:

#!/bin/bash 
# we are assigning file descriptor 3 to input file test.txt 
exec 3< test.txt 
# we are assigning file descriptor 4 to output.txt 
exec 4> output.txt 
# we are using read command to read line from file 
read -u 3 line 
echo "Process id of current process is $$" 
my_pid=$$ 
echo "Currently following files are opened by $0 script :" 
ls -l /proc/$my_pid/fd 
 
# We are closing both files test.txt and output.txt 
exec 3<&- 
exec 4>&- 

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.