Confused with redirection and file descriptors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Confused with redirection and file descriptors
# 1  
Old 05-15-2009
Confused with redirection and file descriptors

Hi all,

I've been looking for the way to send stdout and stderr to different files. Well, actually I really knew how to make it, but I wanted to be sure.
I've found an instruction very interesting which I'm not able to understand:
Quote:
((/path/to/oraMon.pl 2>&1 1>&3 | tee /tmp/errors.log) 3>&1 1>&2 | tee /tmp/output.log) > /tmp/final.log 2>&1
taken from this site. It says it sends stdout to output.log, stderr to errors.log, and both to final.log.
I know 3 is another file descriptor, but which file does it refers? How does it work?
I think I understand:
  • oraMon.pl 2>&1 1>&3: stderr is redirected to stdout and stdout is redirected to 3
  • tee /tmp/errors.log 3>&1 1>&2: takes the standards output previous command, and saves it to a file. However, I don't understand why 3 is redirected to stdout, and sdout to stderr.
I still have another question. The difference between this two instructions:
Quote:
./command > mylog.log 2>&1
./command 2>&1 > mylog.log
I know first one redirects both stdout and stderr to mylog.log. The second one, I read the stderr is redirected to stdout, and stdout is redirected to a file. So I thought both should be the same, but I also read only stdout is saved to a file in the second instruction. Why?

Sorry for this large question, but I'd like to understand redirection and file descriptors.

Thanks a lot.

Albert.
# 2  
Old 05-15-2009
Let's start with the second one:
Code:
./command > mylog.log 2>&1

Here, first stdout is redirected from the TTY to a file. After that, stderr is redirected from the TTY to stdout, which already points to a file.
Code:
./command 2>&1 > mylog.log

In this line, first stderr is redirected to whatever stdout is pointing at that time. Then stdout is redirected to a file, but that doesn't influence the first redirection anymore.

To explain your first line, let's split it up:
Code:
( # start a subshell
  ( # start another subshell
    /path/to/oraMon.pl 2>&1 1>&3 | tee /tmp/errors.log
    # redirect stderr first to stdout, so that tee can read it and then stdout
    # to the new filedescriptor 3 in order to pass it to the outside
  ) 3>&1 1>&2 | tee /tmp/output.log
    # filedescriptor 3 is redirected to stdout, and stdout (as generated by tee)
    # to stderr
) > /tmp/final.log 2>&1
  # redirect stdout to a file and stderr to stdout, capturing both streams

Good shells can create new file descriptors as needed, some even have the ability for TCP or UDP connections without the need for additional programs.
# 3  
Old 05-15-2009
First of all, thanks for your answer.

Quote:
Originally Posted by pludi
Code:
./command 2>&1 > mylog.log

In this line, first stderr is redirected to whatever stdout is pointing at that time. Then stdout is redirected to a file, but that doesn't influence the first redirection anymore.
So, what's the influence of redirect stderr to stdout. It doesn't affect, does it? In other words, any difference between above instruction and:
Quote:
./command 1> mylog.log
which only redirects stdout to a file?

On the other hand, to understand the large instruction, I guess I need to understand better what means "subshell", and how tee works, as well as descriptor 3. I'll google...

Regards,

Albert.
# 4  
Old 05-15-2009
There is a difference:
Code:
./command 2>&1 > mylog.log

redirects stderr to stdout and stdout to a file (without influencing each other). This way, the error messages can be used in a pipe (redirects stdout to stdin)
Code:
./command 1> mylog.log

only redirects stdout, and stderr will always be printed to the console, no matter what.

As for the complex instruction:
  • tee reads from stdin, and prints it to stdout and the file given as an argument. That's useful if you want to see messages as they come and save them to a file.
  • Sub shells allow you to run commands without influencing the parent shell, in this case to direct messages around. Another popular use is to put an independent, long running portion of a script into the background.
  • file descriptor 3 isn't strictly defined. Usually, in a UNIX environment, a program knows 3 file descriptors (fds):
    • 0 is stdin
    • 1 is stdout
    • 2 is stderr
    Just as with a C program you can open additional fds for reading, writing, or both.
# 5  
Old 05-18-2009
Thanks a lot pludi. Now it's quite more clear for me.

Albert.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Questions about file descriptors

Hi, I'm playing with KSH I entered following command in terminal { echo "stdout" >&1; echo "stderr" >&2; } > out And I get only stoud in a new file out. My question is: Where did my stderr vanish ? (5 Replies)
Discussion started by: solaris_user
5 Replies

2. Shell Programming and Scripting

Confused with redirection

Hi Guys, First of all I am not entirely sure if this is the correct way of doing what I need to be done. I have tried to google for some suggestions and so far it got me more confused. Basically what I need is to pipe the output of a program to a function in KSH and inside the function some... (1 Reply)
Discussion started by: maddmaster
1 Replies

3. UNIX for Dummies Questions & Answers

Semaphores and File Descriptors

What is the difference between a file descriptor and a semaphore? My basic understanding is: - a file descriptor is a small positive integer that the system uses instead of the file name to identify an open file or socket. - a semaphore is a variable with a value that indicates the... (1 Reply)
Discussion started by: Mr_Webster
1 Replies

4. HP-UX

exec and file descriptors

Hi, I speak and write english more or less, so I hope my asking be clear. :) In the company I am working, they are using control-m software to lunch shell scripts. So i put this command in all shell scripts: export LOGFILE_tmp=$PRODUC_DATA/tmp/${SCRIPT}_${PAIS}_`date... (0 Replies)
Discussion started by: anamcara
0 Replies

5. UNIX for Dummies Questions & Answers

how to list the files using File Descriptors

hello, I have written a script named listall.sh with the following codes init. #!/bin/bash PATH="/proj/cmon/$1" echo $PATH if ; then echo "Usage: $0 ***" exit 1 else ls -l $PATH/*.sc fi Here there are 3 subdirectories (namely - src, data and jobs)under /proj/cmon, so... (2 Replies)
Discussion started by: shyjuezy
2 Replies

6. UNIX for Advanced & Expert Users

File Descriptors + cron

Hi All, This thread is going to be a discussion basically bringing out more information from the experts on cron jobs and the associated file handles. So, here is the question. There is definitely a constant ' n ' as the maximum number of file handles alloted to a process ' p '. Will... (7 Replies)
Discussion started by: matrixmadhan
7 Replies

7. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies

8. UNIX for Dummies Questions & Answers

file descriptors

i m trying to learn processes in unix and i've been reading this but i don't quite get it. its regarding file descriptors. : each is a part of file pointers, they point to another area. indexes into an Operating system maintained table called "file descriptor table". one table per process. may... (3 Replies)
Discussion started by: a25khan
3 Replies

9. UNIX for Advanced & Expert Users

File Descriptors

Hello all, A few questions on file descriptors ... scenario : Sun Ultra 30 with Sun OS 5.5.1 , E250 with Solaris 2.6 In one of my servers, the file descriptor status from the soft limit and hard limits are 64 and 1024 respectively for root user. Is the soft limit (64) represents the... (3 Replies)
Discussion started by: shibz
3 Replies

10. Programming

File Descriptors

Hi, I have written a daemon process, to perform certain operations in the background. For this I have to close, the open file descriptors, Does anybody know how to find out the number of open file descriptors ? Thanks in Advance, Sheetal (2 Replies)
Discussion started by: s_chordia
2 Replies
Login or Register to Ask a Question