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:

# 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 $$" 
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 O’Reilly online learning.

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