LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash for beginners @ TLDP.ORG, chapter 8, question (https://www.linuxquestions.org/questions/linux-newbie-8/bash-for-beginners-%40-tldp-org-chapter-8-question-705572/)

crispyleif 02-18-2009 02:35 AM

Bash for beginners @ TLDP.ORG, chapter 8, question
 
According to the guide:

ls -l * 2> /var/tmp/unaccessible-in-spool
will redirect standard output of the ls command to the file unaccessible-in-spool in /var/tmp.

I can't get that to be correct, if I issue such a command I get an empty file and standard output on the screen. Ideas ?

ebmi 02-18-2009 02:52 AM

`command 2> file` means that stderr will be redirected to "file"

`command 1> file` is for redirecting stdout to "file"

Cheers.

colucix 02-18-2009 03:14 AM

Yes. There is no message sent to the standard error. When you use redirection to a file, the file is created when the command execution starts, so that it can rest unused if no error message comes out from the execution. If you want to redirect the standard output together with the standard error you can do this:
Code:

ls -l * > /var/tmp/unaccessible-in-spool 2>&1
that is you redirect the standard output to the file, then the standard error to the same place as the standard output. The order of redirections is important!

You can also try a different command to see the standard error file populated by an error message:
Code:

ls -l pippo 2> /var/tmp/unaccessible-in-spool
provided you do not have a file named "pippo" in your current directory.

ebmi 02-18-2009 03:25 AM

Bash also supports

`command &> file`

for redirecting both stdout and stderr into "file". Other shells (such as dash) may or may not support this.

crispyleif 02-18-2009 03:58 AM

Thanks for all help.

I actually agree 100% with my result and with you.

when a command that produces no errer redirects that non-existent
errer to a file, I expect it to be empty.

What confused me was that the guide said that "2>" would redirect stdout,
which it doesn't.

chrism01 02-18-2009 05:17 AM

Must be a typo in the guide. It happens... sometimes you can't trust the book...


All times are GMT -5. The time now is 10:43 PM.