Linux errors to dev null

How do I redirect errors to /dev/null in bash?

We run daily Selenium tests to test our website and extensions. I wrote a script (according to this question) to count the number of passed and failed tests. Here is the script:

#!/bin/bash today=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d"` yesterday=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d" -d "yesterday"` . print_test_results() < declare -i passed_tests=0 declare -i failed_tests=0 declare -i total_tests=0 log_suffix="_$.log" yesterday_logs="$$_[1,2]*$" today_logs="$$_0*$" for temp_file_name in $yesterday_logs $today_logs ; do total_tests+=1 if grep -q FAILED "$temp_file_name" ; then failed_tests+=1 elif grep -q OK "$temp_file_name" ; then passed_tests+=1 else failed_tests+=1 fi done echo "" echo "$test_name - $today" if [ $passed_tests = "0" ]; then echo "$passed_tests passed" echo "$failed_tests failed" else echo "$passed_tests passed" echo "$failed_tests failed" fi echo "$total_tests tests total" echo "" > file_name="chrome_gmail_1_with_extension_test" test_name="Chrome Gmail 1 With Extension Test" print_test_results . 

But the problem is, if the files are not there ( in $yesterday_logs $today_logs ), I get error messages. How do I redirect these error messages to /dev/null? I want to redirect them to /dev/null from the script, and not from the line calling the script — I want this script to never show error messages about files which don’t exist.

Источник

Shell: redirect stdout to /dev/null and stderr to stdout [duplicate]

I saw this interesting question at a comment on cyberciti.biz. That I found I even can’t find a flexible way to do this in one-line command with sh. As far my thought for the solution is:

tmp_file=`mktemp` (./script 2>$tmp_file >/dev/null; cat $tmp_file) | ./other-script rm tmp_file 

But you see, this is not synchronous, and fatally, it’s so ugly. Welcome to share you mind about this. 🙂

@cnicutar It seemed I have a wrong understanding in the start. I supposed 2>&1 will redirect stderr to stdout and the 1>/dev/null will then redirect both them into /dev/null . Well I need to relearn some shell.

3 Answers 3

./script 2>&1 1>/dev/null | ./other-script 

The order here is important. Let’s assume stdin (fd 0), stdout (fd 1) and stderr (fd 2) are all connected to a tty initially, so

0: /dev/tty, 1: /dev/tty, 2: /dev/tty 

The first thing that gets set up is the pipe. other-script’s stdin gets connected to the pipe, and script’s stdout gets connected to the pipe, so script’s file descriptors so far look like:

Next, the redirections occur, from left to right. 2>&1 makes fd 2 go wherever fd 1 is currently going, which is the pipe.

Lastly, 1>/dev/null redirects fd1 to /dev/null

0: /dev/tty, 1: /dev/null, 2: pipe 

End result, script’s stdout is silenced, and its stderr is sent through the pipe, which ends up in other-script’s stdin.

Also note that 1>/dev/null is synonymous to, but more explicit than >/dev/null

Источник

How to Redirect Output and Error to /dev/null in Linux

Learn how to redirect output or error or both to /dev/null in Linux so that it doesn’t show up on the screen.

Читайте также:  Linux changing owner of directory

Imagine this scenario. You run a Linux command and it has a huge output. You don’t want to see its output.

Or, you are using certain Linux commands in your script and you don’t want it to show any output or error on the terminal screen.

For such cases, you can take advantage of the output and error redirection and send them to /dev/null.

Send the output to /dev/null:

Send the error to /dev/null:

Send both output and error to /dev/null:

The /dev/null can be considered a black hole of the Linux file system so whatever you throw there can never see the light again.

Let’s take a detailed look here.

Redirect output to /dev/null in Linux

Let me start off with the basics. You type a command in the terminal is your input (let’s say I executed sudo apt update ).

So I gave input to my system and it will update the directories and it will show me the ongoing processes such as repositories being updated and packages that are outdated now:

In short, it gave the output showing what it did with the command. Now, here is the number designation for each standard data flow:

  • Standard input (stdin) is designated with 0
  • Standard output (stdout) is designated with 1
  • Standard error (stderr) is designated with 2

So now, let me show you how you can redirect standard output to the dev/null :

For example, I will intentionally use the find command which will show output with an error so as I redirect the output to /dev/null, the error should remain intact:

redirect output to dev null

As you can see, when I used the find command without redirecting the output, it showed output worth 1807 lines.

And when I redirected the output, it showed the errors only as it belongs to the standard error stream.

Redirect errors to /dev/null in Linux

As I mentioned earlier, the error stream is designated with 2. So you just have to make a few changes in the above command and you’d be good to go!

To redirect errors, you will have to use > symbol to redirect the data flow with the number 2 indicating it to redirect data flow on standard error.

For example, I will use sudo dnf update on Ubuntu so the error is obvious and on the second window, I will demonstrate how you can redirect the error:

redirect error to /dev/null in linux

Redirect both output and error to /dev/null in Linux

In this section, I will show you how you can redirect both output and the error to /dev/null.

The basic syntax for redirecting output and error to /dev/null is as follows:

command 1> /dev/null 2> /dev/null

Or you can also use the shorter version:

For this example, I will be using the find command to find files in the etc directory in which you’d need sudo privileges to access some sub-directors and when used without sudo, it will throw an error:

redirect output and error to /dev/null in linux

Wrapping Up

This was my take on how you can redirect the output and errors to /dev/null.

And if you want to learn more about redirecting data flow, I would highly recommend you the detailed guide on redirecting data flow in Linux.

Источник

Silencing the Noise: A Guide to Redirecting Output to /dev/null in Linux

While developing a script to automate a task, you might find that the output is printed even though it actually doesn’t require it at all, and because of this, your terminal becomes crowded with unneeded text.

Читайте также:  Загрузка ядра linux в grub команда

The best way to solve this problem is to redirect your script’s output to /dev/null, which will effectively discard the output and prevent it from clogging up your terminal.

So let’s talk about how to redirect the output of a script to /dev/null and what the advantages and disadvantages are of redirecting output to /dev/null.

What is /dev/null/ in Linux, and how will it sink the output of your script?

The obvious question is: what exactly is /dev/null/, and why do we use it? /dev/null is a special file that is basically used in shell scripting to dump or discard output (stdout), errors (stderr), or both (stdout and stderr).

It is often called the “black hole” because data written there can never be retrieved.

It is important to note that you cannot execute /dev/null on your terminal, and because of that, you cannot use /dev/null in piping with another command.

If you don’t believe me, take a look at the output of stat /dev/null.

where you can see that /dev/null is a character device, that it doesn’t take up any space, so its size is 0 bytes, and its type is “character special” with no executable permission to run.

stat of dev null file

Then the obvious follow-up question is, “How exactly can I make use of /dev/null?” You can send the standard output or standard error of a command to /dev/null with the redirection operator and the right file descriptor.

I’m assuming you’re familiar with the concept of “redirection,” which allows you to save the results or failures of a command to a file rather than showing them on the screen. I’ll try to explain some of the concepts here so you can get a sense of how to use them with /dev/null, but if you’re unsure, read this article.

Redirecting output (stdout) to /dev/null in Linux

Let’s start with Stdout, which stands for “standard output” and is the default output channel of a command when the command successfully executes and produces some output.

This output can be directed to any file or even /dev/null if we do not need it. To explain how to redirect output to /dev/null, first see how it works.

I’ll start by showing you what happens if I don’t use /dev/null and then redirect the output to /dev/null. For example, if I want to print the files that are located in the /etc/ssh/ directory, then I can simply use:

$ ls -l /etc/ssh

And here is the output of the above command, which displays the file content of the /etc/ssh directory:

ls command output without redirection

Now, I’ll use the redirection operator (>) with file descriptor “1” to hide the output from the last command.

To do this, I simply type the same command, but add “1>/dev/null” or else use the shorter syntax “> /dev/null” to the end.

$ ls -l /etc/ssh 1> /dev/null or $ ls -l /etc/ssh > /dev/null

Great! As you can see in the result below, I was able to use the redirection operator to hide the output that was first printed to the screen.

ls command output with redirection operator to suppress output

Now it is your turn to complete this task, and you should use the same command syntax I just demonstrated.

Redirecting error output (stderr) to /dev/null in Linux

Stderr, which is short for “standard error,” causes any warnings or errors caused by a command to be displayed on the screen, which is very useful in showing errors and warnings, but it can often be distracting from the output that one is looking for.

Читайте также:  Установить домашнюю директорию linux

Using the redirection operator, you can redirect these errors and warnings to a text file or even completely away using /dev/null.

For example, if I list a directory that doesn’t exist, then of course an error message will get printed on the screen like “ls: cannot access ‘/etc/ssh/unknown’: No such file or directory.”

To suppress this error, I can use the redirection operator as follows:

$ ls /etc/ssh/unknown 2> /dev/null 

The output of the above command is shown below:

ls command error output supress

That’s not hard to understand, is it?

Redirecting output and errors (stdout & stderr) to /dev/null in Linux

Depending on your needs, you can either suppress the error or output the message using the above method, but if you want neither to be displayed on the screen, send both standard output and errors to /dev/null, you need to follow the below steps.

For this purpose, you need to combine standard output (1) and standard errors (2) together using the “>” operator and redirect them to /dev/null, like this: command > /dev/null 2>&1

Let’s take an example. If you don’t want to print the error or success message of the command, you can use the following command to suppress both messages:

$ ls /etc/sshs/ > /dev/null 2>&1

Confused with the above command? The command given above can be hard to understand, but it isn’t too hard to figure out.

According to the availability of the file or directory “ls,” the command will throw an error or output, but it will not display it here because it will be sucked by /dev/null.

And then, 2>&1 will be sucking the output of /dev/null because this “/dev/null” is available on your file system, which can interfere, so 2>&1 will make sure not to print anything.

Instead of using the above command, you can use the one below, which is more simple and easy to use:

$ ls /etc/sshs/ &> /dev/null 

See the results for yourself, with output from both commands showcased below.

Redirecting output and error to dev null

And with that, I shall end.

Quick reference to redirect output to /dev/null

  • Use the following command to direct standard (stdout) output messages to /dev/null:
    • $ command 1> /dev/null
    • $ command > /dev/null
    • $ command 2> /dev/null
    • $ command &> /dev/null

    Wrap up

    This can be a great way to ensure that your scripts are running smoothly and efficiently, as the output will not clutter up your terminal

    With this in mind, it’s important to remember that redirecting output to /dev/null has its uses, but if you’re not careful, it can cause problems because it throws away all output and makes it hard to debug.

    Anyway, if there is something I need to consider, please use the space below to provide any additional information or feedback.

    Till then, put your feet up and relax. As always, the next article will be up shortly.

    A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.

    Источник

Оцените статью
Adblock
detector