How to catch the error message in Linux with SHELL commands

Posted on 2011-10-24
Last Modified: 2012-05-12
Want to process the error message from issuing commands. For example,

ping 10.10
connect: Network is unreachable

My question is how to get "connect: Network is unreachable" out.

I tried to use the similar idea like

ls > test.log

I can see a lot of files/directories with "cat test.log", but it did not work for

ping 10.10 >test.log

If I see it, test.log is an empty file.

Which commands can catch the error message or pipe somewhere???
Question by:jl66
    LVL 11

    Assisted Solution

    This does what I think you are looking for :

    for ip in 192.168.1.{1..10}; do
        ping -c 1 -t 1 $ip > /dev/null 2> /dev/null  # ping and discard output
        if [ $? -eq 0 ]; then  # check the exit code
            echo "${ip} is up" >>test.log # display the output
            echo "${ip} is down"

    Open in new window

    LVL 19

    Accepted Solution

    A Unix command generates normal output, and error output, referred to as stdout and stderr.

    When you do "ping 10.10 > test.log"  you are redirecting stdout to the log file.

    This is great if the host responds, but it doesn't help you if the command fails.  For that you need to do:

    ping 10.10 2> /test.log

    See, 1> is stdout, and 2> is stderr.  You can direct both to different places like:

    ping 10.10 1>success.log 2> error.log

    Or if you like, you can combine them like this:

    ping 10.10 1> ping.log 2>&1

    One of those should get you what you need, good luck!

    Author Closing Comment

    Thanks a lot for the solution. very helpful.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now