Redirection does not work correctly, 2> / dev / null becomes 2> / dev / null, and stderr does not redirect

I hope someone can help me figure out what settings I may need to overwrite. I work on a Unix terminal server using Linux Xterm linux. Each time I use a command like grep "blah" 2> /dev/null on the command line, the command runs like grep "blah" 2 > /dev/null , and of course the redirection fails. Xterm version - X.Org 6.8.99.903 (238)

I can’t update or install anything, this is a blocked production server.

Thanks for any help and coverage on the topic, this makes my grep useless on high levels of directories with recursion.

+4
source share
2 answers

This is Bourne shell syntax and it does not work in c-shell.

The best you can do is

  ( command >stdout_file ) >&stderr_file 

Where do you get stdout for one file and stderr for another. Redirecting just stderr is not possible.

+9
source

In the commentary, you say "A little note, this is csh." This is not a secondary observation that the cause of the problem. xterm is just a terminal emulator, not a shell; all he does is set up a window that provides text input and output. csh (or bash , or ...) is a shell, a program that interprets the commands you enter.

csh has a different syntax for redirection and does not allow redirecting only stderr. command > file redirects stdout; command >& file redirects both stdout and stderr.

You say that the system does not have bash, but it has ksh. I suggest just using ksh; it will be much more familiar to you. Both bash and ksh are derived from the old Bourne shell.

All (?) Unix-like systems will have a Bourne shell installed as /bin/sh . Even if you use csh (or tcsh?) As your interactive shell, you can still call sh, even in single-line mode. For instance:

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

sh is invoked, which in turn is invoked by command and only redirects its stderr to /dev/null .

The purpose of the interactive shell (mainly) is to allow you to use other commands available on the system. sh , or any shell, can be used as another command.

+3
source

Source: https://habr.com/ru/post/1489359/


All Articles