Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect not working correctly, 2> /dev/null becomes 2 > /dev/null and stderr doesn't get redirected

I am hoping someone can help me figure out what setting I might need to overwrite. I am working on a Unix terminal server, running a Linux Xterm linux shell. Everytime I use a command like grep "blah" 2> /dev/null at the shell prompt, the command is run as grep "blah" 2 > /dev/null and needless to say the redirection fails. xterm version is X.Org 6.8.99.903(238)

I can not update or install anything, this is a locked down production server.

Thanks for any help and illumination on the topic, it is making my grep useless at high directory levels with recursion.

like image 685
squidpie Avatar asked Dec 15 '22 09:12

squidpie


2 Answers

That's Bourne shell syntax, and it doesn't work in c-shell.

The best you can do is

    ( command >stdout_file ) >&stderr_file

Where you get stdout to one file, and stderr to another. Redirecting just stderr is not possible.

like image 60
Lennart Regebro Avatar answered Apr 28 '23 14:04

Lennart Regebro


In a comment, you say "A minor note, this is csh". That's not a minor note, that's the cause of the problem. xterm is just a terminal emulator, not a shell; all it does is set up a window that provides textual input and output. csh (or bash, or ...) is the shell, the program that interprets the commands you type.

csh has different syntax for redirection, and doesn't let you redirect just stderr. command > file redirects stdout; command >& file redirects both stdout and stderr.

You say the system doesn't have bash, but it does have ksh. I suggest just using ksh; it will be a lot more familiar to you. Both bash and ksh are derived from the old Bourne shell.

All (?) Unix-like systems will have a Bourne-like shell installed as /bin/sh. Even if you're using csh (or tcsh?) as your interactive shell, you can still invoke sh, even in a one-liner. For example:

sh -c 'command 2>/dev/null'

will invoke sh, which in turn will invoke command and redirect just its stderr to /dev/null.

The purpose of an interactive shell is (mostly) to let you use other commands that are available on the system. sh, or any shell, can be used as just another command.

like image 24
Keith Thompson Avatar answered Apr 28 '23 14:04

Keith Thompson