LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   io redirection doesn't work? 2>&1 >> logfile.log (https://www.linuxquestions.org/questions/linux-general-1/io-redirection-doesnt-work-2-and-1-logfile-log-331775/)

Thinking 06-09-2005 03:11 AM

io redirection doesn't work? 2>&1 >> logfile.log
 
hiho@ll

i tried
command 2>&1 >> logfile.log
to log everything (stdout and stderr) to a logfile
i only get stdout!
i also tried
>> logfile 2>&1
the same!

command &>> logfile.log
i get an error
&> logfile.log
only gets stdout

anybody knows how it REALY works?
i searched the forum and the web and everybody thinks it's like the above methods

but they don't work

thx@ll

Simon Bridge 06-09-2005 04:20 AM

Intreguing - I'd have thought that 2>&1 would redirect stderr into stdout - then you append stdout ... then >>file just sends stdout to the file ... so you're piping stderr to the screen and stdout to a file. To send them both you need two commands ... I'll see what google says just to check my suppositions.

<googles> I see what you mean ... however: I see it is OS/2 which redirects like this? ... did you see this one:http://www.cpqlinux.com/redirect.html ?

It says:
Quote:

# Order of redirecting

* Method 1
Code:

[root@server /root]# cmd 2>&1 1>outfile.txt
#The above command causes the original stderr to go to stdout,
# and causes the original stdout to go to outfile.txt.
#Notice at the time that fd (file descriptor) 2 is redefined, fd 1 still pointed to stdout; therefore, fd 2 will continue to point to stdout independently of what later happens to fd 1. Basically fd 2 copies the item (or address) that fd 1 is pointing to.

* Method 2
#The following command causes both the original stdout and stderr to go to outfile.txt.
Code:

[root@server /root]# cmd 1>outfile.txt 2>&1
#Notice that 1=outfile.txt when 2 is redefined, so 2 goes to outfile.txt too (the two
# channels are combined).
So you do:
Code:

cmd 1>>logfile.log 2>&1
... of course, why not just:
Code:

cmd 2>>logfile.log 1>>logfile.log
? It's easier to read after all...

Thinking 06-09-2005 08:29 AM

do you mean most bash scripting tutorials are wrong or not correct?

because http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html says
3.6 Sample: stderr and stdout 2 file

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.

rm -f $(find / -name core) &> /dev/null


This (thinking on the cron entry) will delete every file called 'core' in any directory. Notice that you should be pretty sure of what a command is doing if you are going to wipe it's output.

and &> didn't work too

and i never found a script which says it only works for OS/2

i tryed your solution and it worked

what did you search on google to find this?

thx

foo_bar_foo 06-09-2005 05:16 PM

Quote:

Originally posted by Thinking
do you mean most bash scripting tutorials are wrong or not correct?

certainly that page you pointed to has an example that has nothing to do with the title of the section

the point you missed was that redirection works from right to left as Simon Bridge has pointed out so expertly

so the proper command is

command >logfile.log 2>&1
or
command 1>logfile.log 2>&1
or
command 2>logfile.log 1>&2

google for a tutorial called "rute tutorial"

Thinking 06-10-2005 02:35 AM

i'm not sure if i know what you mean

1. rute tutorial looks very good ;-) thx

2. the tutorial i search (the advanced bash scripting tutorial) explained io redirection (and it didn't work)
--> i thought there was an failure in the tutorial
so, why shouldn't the bash tutorial have anything to do with my topic?
because:

3. i tried some example on a suse distri (very new, don't know exactly the version)
i tried: cat * | grep "nothingfound" >> test.out 2>&1
bash did it, but there was nothing in test.out and cat throwed a few error messages (because it tried to cat a directory, which is what i wanted, because of the 2>&1 redirection ;-))
BUT: cat * >> test.out 2>&1 | grep "nothingfound"
works (i know it's not very usefull example)
i understand why, but it looks now as if 2>&1 works also using linux??

i thought it should "only" work on OS/2

if the above example works why doesn't work this:
./anyprogram >> test.out 2>&1
anyprogram is a self written prog (C++), which uses system command to execute some scripts, which could make error messages
but if i cat test.out there is no Error message?

is io redirection really as that complicated as it looks like (for me ):D

thx@ll

Simon Bridge 06-10-2005 04:31 AM

Well - I googles the line you tried "command 2>&1" and when I saw that all the tutorials had a little tag on the end (OS/2) just quite like - I appended "linux" to the search term. The site I told about was something like second.

You have to read these things carefully - the tutorials weren't wrong as such, they just included things which don't work in linux. They didn't claim anything for linux BTW.


All times are GMT -5. The time now is 07:43 AM.