LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 03-03-2010, 07:04 AM   #1
ahmad.zuhd
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Rep: Reputation: 0
Question File Descriptor redirection and duplication


i've checked the link, and it makes it better. but it doesn't include all the information. i'll continue searching the internet. However i have seen an example of creating a fd:

Code:
exec 5<&1
echo "TEST" >&5
exec 5>&-

as in the page, this was intended to redirect the stdout to the fd 5 and create it, and close it. i have the following questions:
- what is exactly the meaning of second command? is it to redirect the command stdout "test" to the fd 5? and how i can see the contents of the fd 5?
- in the first command, why the < is used instead if > and what is the difference between the below two commands as in the info bash *Redirection section

It will be helpful if anyone could include a graph for file descriptor before and after different command execution.

Quote:
3.6.7 Duplicating File Descriptors
----------------------------------

The redirection operator
[N]<&WORD
is used to duplicate input file descriptors. If WORD expands to one
or more digits, the file descriptor denoted by N is made to be a copy
of that file descriptor. If the digits in WORD do not specify a file
descriptor open for input, a redirection error occurs. If WORD
evaluates to `-', file descriptor N is closed. If N is not specified,
the standard input (file descriptor 0) is used.

The operator
[N]>&WORD
is used similarly to duplicate output file descriptors. If N is not
specified, the standard output (file descriptor 1) is used. If the
digits in WORD do not specify a file descriptor open for output, a
redirection error occurs. As a special case, if N is omitted, and WORD
does not expand to one or more digits, the standard output and standard
error are redirected as described previously.
 
Old 03-03-2010, 09:13 AM   #2
hda7
Member
 
Registered: May 2009
Distribution: Debian squeeze
Posts: 232

Rep: Reputation: 30
I've played around, and this is what I've come up with:

cmd >word redirects stdout to file word
cmd >&num redirects stdout to file descriptor num
cmd num>word changes file descriptor num to point to file word
cmd num1>&num2 changes file descriptor num1 to point to where file descriptor num2 points
if cmd is "exec", the redirection applies to the shell, otherwise it applies only to that command.
 
Old 03-07-2010, 02:06 AM   #3
ahmad.zuhd
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: 0
in general, i need to know what is the difference between

Quote:
[N]<&WORD
is used to duplicate input file descriptors.

[N]>&WORD
is used similarly to duplicate output file descriptors
for example, if we apply the below direction to the shell by executing them with exec, what is the effect:
4<&1
4>&1
1>&4
1<&4
0>&4
0<&4
4>&0
4<&0
 
Old 03-08-2010, 08:18 AM   #4
hda7
Member
 
Registered: May 2009
Distribution: Debian squeeze
Posts: 232

Rep: Reputation: 30
Use num>something when you intend to write to your file descriptor, use num<something when you intend to read from it. The direction of the arrow determines how bash will open the file (see man 2 open).
 
Old 03-09-2010, 12:55 AM   #5
ahmad.zuhd
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: 0
i'm asking about the special case of redirection (using redirection between file descriptors). for the FD vs File usage it is clear.
 
Old 03-09-2010, 08:51 AM   #6
hda7
Member
 
Registered: May 2009
Distribution: Debian squeeze
Posts: 232

Rep: Reputation: 30
As far as I can tell, bash duplicates the file descriptor (see man 2 dup), and I think that the arrow doesn't matter. I would still follow the same guidelines as for files though, so for duplicating stdin (0) use <, for duplicating stdout (1) or stderr (2) use >.
 
  


Reply

Tags
descriptor


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Perl] Idea on statistics for duplication file contents Kunsheng Linux - Software 1 10-24-2009 02:40 AM
[Perl] Idea on statistics for duplication file contents Kunsheng Programming 2 10-23-2009 11:42 PM
Too many file open - How can we define Max limit of file descriptor janeshb Linux - Server 11 09-29-2009 10:41 AM
File duplication (sort of) abharsair Linux - Server 8 01-26-2009 10:08 AM


All times are GMT -5. The time now is 11:21 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration