Author Topic: [SOLVED] Anomalies when directing terminal output to a file  (Read 360 times)

Offline ternor

  • Hero Member
  • *****
  • Posts: 1793
[SOLVED] Anomalies when directing terminal output to a file
« on: January 01, 2013, 11:14:05 PM »
Hello.  Until recently I was able to use a line such as the following in a terminal to create a text file containing the terminal output:

Code: [Select]
kwrite --help > /data/lib/software/kwrite
For kwrite the code still works.  I forget the other names I have tried recently but I have today tried the following:

Code: [Select]
mylivecd --help > /data/lib/software/mylivecd
That resulted in the output appearing in the terminal and an empty file 'mylivecd'.  I have tried using >> to append the output to the empty file but that doesn't work either.

Can anyone shed light on this new development?
« Last Edit: January 02, 2013, 03:47:08 PM by ternor »

Offline TerryN

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 723
Re: Anomalies when directing terminal output to a file
« Reply #1 on: January 02, 2013, 01:51:02 AM »
Linux has 2 output streams called stdout and stderr.

Normal program output is written to stdout and usually error messages and the like go to stderr.  By default both streams are sent to your terminal (window) but can be redirected to files using the > and >> characters.
When you do your redirect as > or >> you are redirecting stdout
mylivecd is writing the help text to stderr so you need to do 2> or 2>> which will redirect stderr (note: no space between 2 and >)

Terry
« Last Edit: January 02, 2013, 04:40:44 AM by TerryN »
Dell E521 - AMD 64 X2 5000+, 4GB RAM, ATI X1300 graphics
PCLinuxOS 2013 (KDE)
|Twitter|

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10621
  • MLUs Forever!
Re: Anomalies when directing terminal output to a file
« Reply #2 on: January 02, 2013, 04:10:33 AM »
Thanks TerryN ....  I also had come across this but never followed it up and did not know why.

Now all I have to do is remember it in the future ....  ;)

Is there a reason why some output to stderr and not stdout?
MLUs rule the roost!

Linux XPS 3.2.18-pclos2.pae.bfs  32 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline TerryN

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 723
Re: Anomalies when directing terminal output to a file
« Reply #3 on: January 02, 2013, 04:52:51 AM »
Is there a reason why some output to stderr and not stdout?

A lot of commands output the help text if you specify an incorrect option or parameter.  If the command output is redirected you wouldn't see the help text (without looking in the file).

Terry
Dell E521 - AMD 64 X2 5000+, 4GB RAM, ATI X1300 graphics
PCLinuxOS 2013 (KDE)
|Twitter|

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10621
  • MLUs Forever!
Re: Anomalies when directing terminal output to a file
« Reply #4 on: January 02, 2013, 05:06:25 AM »
Is there a reason why some output to stderr and not stdout?

A lot of commands output the help text if you specify an incorrect option or parameter.  If the command output is redirected you wouldn't see the help text (without looking in the file).

Terry

Does that mean that all commands that output the help info when an error is met do so to stderr, and never to stdout, even if -help is specifically called?

Sorry for the multiple questions ...... trying to get it sorted in my head after only one cup of coffee .....


MLUs rule the roost!

Linux XPS 3.2.18-pclos2.pae.bfs  32 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline TerryN

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 723
Re: Anomalies when directing terminal output to a file
« Reply #5 on: January 02, 2013, 05:12:44 AM »
Does that mean that all commands that output the help info when an error is met do so to stderr, and never to stdout, even if -help is specifically called?

It is entirely up to the developer.  Different developers have different views.  If your command generates output which is likely to be redirected (i.e. most command line tools) then it makes sense to send the help text to stderr (that's MY view  ;D).

Developers are notoriously lazy and would not write 2 different routines for --help and invalid parameter cases  :)

Terry.
« Last Edit: January 02, 2013, 05:42:22 AM by TerryN »
Dell E521 - AMD 64 X2 5000+, 4GB RAM, ATI X1300 graphics
PCLinuxOS 2013 (KDE)
|Twitter|

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10621
  • MLUs Forever!
Re: Anomalies when directing terminal output to a file
« Reply #6 on: January 02, 2013, 05:30:24 AM »
Thanks  ;)

MLUs rule the roost!

Linux XPS 3.2.18-pclos2.pae.bfs  32 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Bald Brick

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 6372
  • I'm going South
Re: Anomalies when directing terminal output to a file
« Reply #7 on: January 02, 2013, 08:58:18 AM »
Does that mean that all commands that output the help info when an error is met do so to stderr, and never to stdout, even if -help is specifically called?

It is entirely up to the developer.  Different developers have different views.  If your command generates output which is likely to be redirected (i.e. most command line tools) then it makes sense to send the help text to stderr (that's MY view  ;D).

It does make sense, but it means that output from commands with the --help option is treated differently depending on the command in question. To me this sounds like a bug in those commands that send the output to standard error.

With the --help option most binary applications (as for instance muungwana's zuluCrypt-cli) send output to standard output. So do pinoc's scripts lomanager and addlocale (lusbc).

But some scripts, like mylivecd, send it to standard error. And this is inconsistent. (So perhaps standard error and standard output need some semantic standardisation.)

Quote
Developers are notoriously lazy and would not write 2 different routines for --help and invalid parameter cases  :)

Terry.

You said it.  ;) ;D
« Last Edit: January 02, 2013, 02:26:37 PM by Bald Brick »
Feed the trolls!
They need it!

AMD Athlon 7450 Dual-Core Processor, 7.80 GiB RAM, Nvidia GeForce GT 120/PCIe/SSE2, OpenGL/ES-version: 3.3 0 NVIDIA 295.40, SBx00 Azalia (Intel HDA) soundcard, ‎Logitech B500 webcam, SAA7146 DVB card, HDDs: Seagate 250824AS, Western Digital WD10EAVS-00D

Offline ternor

  • Hero Member
  • *****
  • Posts: 1793
Re: Anomalies when directing terminal output to a file
« Reply #8 on: January 02, 2013, 02:49:06 PM »
Thanks for the replies.  2> does the job.  I assume it can be used whenever writing output to a file?

Offline TerryN

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 723
Re: Anomalies when directing terminal output to a file
« Reply #9 on: January 02, 2013, 02:58:44 PM »
Thanks for the replies.  2> does the job.  I assume it can be used whenever writing output to a file?

Yep, it's the same as > except that it redirects the stderr stream.
If you want to redirect both streams to the same file you can use >outfile 2>&1

Terry
« Last Edit: January 02, 2013, 04:43:35 PM by TerryN »
Dell E521 - AMD 64 X2 5000+, 4GB RAM, ATI X1300 graphics
PCLinuxOS 2013 (KDE)
|Twitter|

Online Bald Brick

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 6372
  • I'm going South
Re: Anomalies when directing terminal output to a file
« Reply #10 on: January 02, 2013, 03:09:07 PM »
Thanks for the replies.  2> does the job.  I assume it can be used whenever writing output to a file?

It only works if the output is sent to stderr (standard error). It doesn't work if the output is sent to stdout (standard output). That is the problem.

If you want to write all output to a file you can nowadays use something like
Code: [Select]
mylivecd --help &> /data/lib/software/mylivecd
TerryN's traditional syntax naturally also works:
Code: [Select]
mylivecd --help > /data/lib/software/mylivecd 2>&1
« Last Edit: January 02, 2013, 03:16:11 PM by Bald Brick »
Feed the trolls!
They need it!

AMD Athlon 7450 Dual-Core Processor, 7.80 GiB RAM, Nvidia GeForce GT 120/PCIe/SSE2, OpenGL/ES-version: 3.3 0 NVIDIA 295.40, SBx00 Azalia (Intel HDA) soundcard, ‎Logitech B500 webcam, SAA7146 DVB card, HDDs: Seagate 250824AS, Western Digital WD10EAVS-00D

Offline ternor

  • Hero Member
  • *****
  • Posts: 1793
Re: Anomalies when directing terminal output to a file
« Reply #11 on: January 02, 2013, 03:34:50 PM »
Thanks.  More thinking is required on my part.  I used mylivecd --help 2> /data/lib/software/mylivecd which, I now gather, is incorrect (even though it produced a help file).  The two commands you list also work.
« Last Edit: January 02, 2013, 03:44:41 PM by ternor »

Offline TerryN

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 723
Re: Anomalies when directing terminal output to a file
« Reply #12 on: January 02, 2013, 03:44:08 PM »
How is it incorrect if it produced a help file  ??? ;D

Terry
Dell E521 - AMD 64 X2 5000+, 4GB RAM, ATI X1300 graphics
PCLinuxOS 2013 (KDE)
|Twitter|

Offline ternor

  • Hero Member
  • *****
  • Posts: 1793
Re: Anomalies when directing terminal output to a file
« Reply #13 on: January 02, 2013, 03:46:39 PM »
OK.  Now I have 3 options but the first apparently only works if the application outputs to stderr, so I shall use the first option quoted by Bald Brick.  My thanks to both of you.

EDIT: The code kwrite --help 2> /data/lib/software/kwrite1 produces output in the terminal and an empty kwrite1 file.
« Last Edit: January 02, 2013, 04:04:10 PM by ternor »

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2231
Re: Anomalies when directing terminal output to a file
« Reply #14 on: January 02, 2013, 06:48:05 PM »
OK.  Now I have 3 options but the first apparently only works if the application outputs to stderr, so I shall use the first option quoted by Bald Brick.  My thanks to both of you.

EDIT: The code kwrite --help 2> /data/lib/software/kwrite1 produces output in the terminal and an empty kwrite1 file.
So apparently, kwrite sends the help file to stdout, and true errors to stderr.  As someone said, it's the developers choice. 

If the normal response (of the given program) to an invalid parameter is simply to print the help, then it makes sense to write the help to stderr, in case the normal output has been redirected.  If each possible error has it's own error message, then it makes sense to write the help to stdout, and the error messages to stderr.

Retired Senior Chief, Retired Software Engineer, Active GrandPa