Operating System - Linux
1753719 Members
5142 Online
108799 Solutions
New Discussion юеВ

C-Shell Redirection Chicanery

 
SOLVED
Go to solution
Ralph Grothe
Honored Contributor

C-Shell Redirection Chicanery

Hello,

first of all, I must confess that I am not even a casual user of the csh and siblings.
My surprise when trying to extrapolate from Bourne shell redirection basics seems to endorse my deeply rooted discomfort with this family of shells.

I was asked by a user why, simple stderr suppression by the usual 2>/dev/null idiom, didn't work for him,
which I first couldn't understand until I noticed that he was using a csh.
He simply wanted to run a find from / for some file name but didn't want to see the many permission denied messages clutter his output.

A quick glance at the manpage of csh didn't really bring the revelation of a csh equivalent idiom.
Because when I tried

%> find / -name \*txt >& /dev/null

of course, both stderr as well as stdout were suppressed.

After many surprisingly futile attempts
I suggested to him to simply stay away from csh for tasks like this and temporarily enter a Bourne shell or run the command in a Bourne subshell.

The closest I got was with a command like this

%> (find / -name \*txt >/tmp/txt-files) >& /dev/null

but this isn't equivalent to

$ find / -name \*txt 2>/dev/null

as it requires tentative storage of results in a file.

I bet there must be a better way which avoids the interim file in csh too.
Maybe some veteran SunOS user who grew up wih csh could give me a hint?

Regards
Ralph
Madness, thy name is system administration
8 REPLIES 8
Dennis Handly
Acclaimed Contributor

Re: C-Shell Redirection Chicanery

>I must confess that I am not even a casual user of the csh and siblings.

Exactly. The first thing you should do in the scummy cshell is the "ksh" or "sh" or exit command. :-)

>enter a Bourne shell or run the command in a Bourne subshell.

Don't even think of using Bourne. Use a real shell, sh or ksh. The Bourne shell is why I got sucked in the scummy cshell dark side. :-(

>I bet there must be a better way which avoids the interim file in csh too.

I don't think so. I think your solution was the trick.
Ralph Grothe
Honored Contributor

Re: C-Shell Redirection Chicanery

Hi Dennis,

with Bourne shell I implicitly meant modern Posix shells like HP-UX's standard shell, ksh, bash etc. which all have Bourne shell as ancestor.
Madness, thy name is system administration
Dennis Handly
Acclaimed Contributor

Re: C-Shell Redirection Chicanery

>which all have Bourne shell as ancestor.

Yes, but if it doesn't have a history mechanism, I won't use it.
Steven Schweda
Honored Contributor

Re: C-Shell Redirection Chicanery

I generally use bash these days, but a Google
search for:
csh redirect stderr OR error
found many things, including ("Feeling
Lucky"?):

http://www.faqs.org/faqs/unix-faq/faq/part2/section-9.html

which says:

Date: Mon, 26 Oct 1992 20:15:00 -0500

2.9) How do I redirect stdout and stderr separately in csh?

In csh, you can redirect stdout with ">", or stdout and stderr
together with ">&" but there is no direct way to redirect stderr
only. The best you can do is

( command >stdout_file ) >&stderr_file

which runs "command" in a subshell; stdout is redirected inside
the subshell to stdout_file, and both stdout and stderr from the
subshell are redirected to stderr_file, but by this point stdout
has already been redirected so only stderr actually winds up in
stderr_file.

If what you want is to avoid redirecting stdout at all, let sh
do it for you.

sh -c 'command 2>stderr_file'


Boy, this new Inter-Web thing is a miracle.
Ralph Grothe
Honored Contributor

Re: C-Shell Redirection Chicanery

Hi Steven,

the csh redirection by use of a subshell (viz. using parentheses) was what I also have found out.
But unlike you I found it the old-fashioned analogue way in my old copy of "Unix in a Nutshell" book (must be from the days when BSD and SunOS was en vogue and everyone used csh).
I was looking for a pure csh method, other than calling a Bourne shell and passing the command to -c (what I also have suggested to my colleague),
that would avoid dumping the sought output to a file instead of stdout.

Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor
Solution

Re: C-Shell Redirection Chicanery

Hi Ralph:

I can't help pointing you to the classic article by a gentleman whose name is well known to you:

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

Perhaps, some of your co-workers would benefit from reading it.

As for the Bourne shell, a version on HP-UX can be found as '/usr/old/bin/sh'. The POSIX conformant shell, as you know, is the standard shell and is '/usr/bin/sh' as a dynamically-linked variant with '/sbin/sh' using static linkages for root and single-user mode when '/usr' isn't mounted.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: C-Shell Redirection Chicanery

Bourne may be the ancestor of the POSIX standard shells like ksh and bash but outside of HP-UX, the "sh" shell is the primitive Bourne shell which leads to a lot of confusion with new users. The most standard Unix shells today are POSIX-based, with ksh-1988 the most common. HP's POSIX shell is ksh-1988 based (with some enhancements) and HP-UX also supplies ksh-1993, cleverly hidden as /usr/dt/bin/dtksh. BASH is also POSIX compliant so scripts are highly interchangeable. Probably the most common issue between platforms is with echo and print where echo is often an external command rather than built-in. echo is deprecated in the man page for sh-posix, dtksh and ksh.


Bill Hassell, sysadmin
Ralph Grothe
Honored Contributor

Re: C-Shell Redirection Chicanery

James,

many thanks for reminding me of this great article.
Of course do I know its author from many parts of the Perl installed POD,
as well as co-author of the famous Perl Cookbook.


Bill,

many thanks too for the short synopsis of POSIX shell revisions.
For the most part I already abandoned the built-in echo in favour of the more portable printf (especially between HP and Linux where for the latter things like interpolated escape chars like simple \n require the escape option -e for echo)



Madness, thy name is system administration