0

I was trying to use file descriptor for printing the lines in the file

#!/usr/bin/ksh
exec 3<&0
exec 0< file.txt
while read LINE
do
   echo “$LINE”
done

and I am done with my code ,it prints the output as like my wish

I forgot to reset the file descriptor of 0

exec 0<&3

what will happen ?

  1. The descriptor scope will last only inside the program or entire unix shell?
  2. How we come to know which value is holding which file descriptor (printing the values of file descriptor using shell)
  3. It will automatically reset the system (AIX) is restarted ?
  • 1
    It affects the current shell. Closing the terminal will do it, but you don't even need that. exec 3<&0 brings it back - you've only duped it. – mikeserv Mar 18 '14 at 5:15
  • If I am writing a huge program and using a file descriptor for different values ,and how to print which value is holding which file decriptor – Jeyanthinath Mar 18 '14 at 5:17
  • Depends on how you assign them. The most robust method, as far as I know, would be to consolidate all calls for new file descriptors into a single function that can dish them out and remember where they've gone. Then have it clean them up when it thinks its time. – mikeserv Mar 18 '14 at 5:30
3

File descriptors belong to a process: the current process, that is. Immediately when you exit a process, the assignments have no effect. Even a subshell (or any child process) that inherit the file descriptors, have from the fork() call their own copies of the file descriptors (a file descriptor is just a number pointing to an IO resource in linux kernel). It's just like open files - opening a file in one process doesn't make it open for everyone and if you don't close it yourself, linux closes it and cleans up when the process terminates.

1

How we come to know which value is holding which file descriptor (printing the values of file descriptor using shell)

You can list all files descriptors of a process using via the /proc filesystem, using

ls -l /proc/<PID>/fd

Example:

$ ls -l /proc/2218/fd
total 0
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 0 -> /dev/null
l-wx------ 1 setacinq setacinq 64 Mar 14 09:29 1 -> /dev/null
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 18 -> pipe:[14524]
l-wx------ 1 setacinq setacinq 64 Mar 14 09:29 19 -> pipe:[14524]
l-wx------ 1 setacinq setacinq 64 Mar 14 09:29 2 -> /home/setacinq/.xsession-errors
lrwx------ 1 setacinq setacinq 64 Mar 14 09:29 22 -> anon_inode:[eventfd]
lrwx------ 1 setacinq setacinq 64 Mar 14 09:29 23 -> anon_inode:[eventfd]
lrwx------ 1 setacinq setacinq 64 Mar 14 09:29 28 -> socket:[14566]
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 30 -> /usr/share/unity/lenses/commands/commands.lens
lrwx------ 1 setacinq setacinq 64 Mar 14 09:29 32 -> socket:[11753]
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 35 -> /usr/share/unity/lenses/music/music.lens
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 36 -> /usr/share/unity/lenses/files/files.lens
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 37 -> /usr/share/unity/lenses/applications/applications.lens
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 38 -> /usr/share/unity/lenses/video/video.lens
lr-x------ 1 setacinq setacinq 64 Mar 14 09:29 8 -> anon_inode:inotify
lrwx------ 1 setacinq setacinq 64 Mar 14 09:29 9 -> /home/setacinq/.config/compiz-1/compizconfig/done_upgrades

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.