#!/bin/bashRecently looking at the shell, a variety of puzzles, but to solve the feeling of confusion is still very good. Needless to say, Linux uses file descriptors to identify each file object. The file descriptor is a non-negative integer that uniquely identifies the open file in the session. Each procedure can have a maximum of 9 file descriptors. Standard file descriptor used by Linux: standard input stdin is 0, standard output stdout is 1, standard error output stderr is 2. And we can create the file descriptor 3~8 ourselves. The piece of code you'll see today:
1 #!/bin/bash
2 exec 3>&1
3 exec 1>test
4 echo "Something to output file"
....
....
5 exec 1>&3
6 echo "Now things should is back"The second line of code uses the file descriptor 3 to point to the location of the file descriptor 1, the location of the standard output, to hold the location of the standard file descriptor 1. The third line of code then redirects the file descriptor 1 to the file test before executing the subsequent code. The file descriptor 1 is again pointed to its original location when the code is executed to line 5th. This method is to temporarily redirect output in the script and then restore the output to the usual set of methods ~ or will often use, whistling ~. Or that sentence:
Where There is a shell,there is a way!
#exit 0
Shell Redirection File Descriptor