differentiate between standard output and standard error?

differentiate between standard output and standard error?

Post by Edwar » Fri, 21 Dec 2001 23:34:26



Is there a way to differentiate between the output resulting from an error
generated by a command and the successful completion of a command (both
normally written to the terminal)  I've heard about sterr and stout and
think they are both written to the terminal.

The command (dig) that I want to use in a script could:
1 successfully return results
2 successfully fail to return results because no results are available
3 fail altogether due to network problems / unavailability of remote servers
etc

I presume 1 + 2 are stout and 3 is sterr    - can BASH pickup this
difference?

Thanks,

Edward.

Background:

I'm planing a script that will use the dig command to check where a domain
name is delegated to.

for $domain from a list of domains, I'll run the command:
dig $domain NS.    (checks what DNS servers a domain is delegated to)

If the command fails (sterr) I'll write the domain to a failure file
If the command runs successfully, I'll grep the output for our DNS servers
and, if found, write $domain to a file localDNS.
If our DNS servers are not found in the grep I'll write $domain to a file
remoteDNS.
For a domains in remoteDNS, I'll establish the actual DNS server from the
output of the dig command and specify it as a variable $dnsserver.
$dnsserver will be used in further DNS commands of the form:

the results will be written to the remoteDNS file.

etc etc

 
 
 

differentiate between standard output and standard error?

Post by Edwar » Sat, 22 Dec 2001 00:00:00


I've just read about file descriptors
you can direct standard outout (1) with >
you can direct standard error (2) with 2>

so I could dig $domain NS > tmp 2> failure

tmp would then only contain the standard output of the dig(?) and I could
then grep tmp for the name servers. Tmp would be written over on each loop
(could I say on each iteration?)


> Is there a way to differentiate between the output resulting from an error
> generated by a command and the successful completion of a command (both
> normally written to the terminal)  I've heard about sterr and stout and
> think they are both written to the terminal.

> The command (dig) that I want to use in a script could:
> 1 successfully return results
> 2 successfully fail to return results because no results are available
> 3 fail altogether due to network problems / unavailability of remote
servers
> etc

> I presume 1 + 2 are stout and 3 is sterr    - can BASH pickup this
> difference?

> Thanks,

> Edward.

> Background:

> I'm planing a script that will use the dig command to check where a domain
> name is delegated to.

> for $domain from a list of domains, I'll run the command:
> dig $domain NS.    (checks what DNS servers a domain is delegated to)

> If the command fails (sterr) I'll write the domain to a failure file
> If the command runs successfully, I'll grep the output for our DNS servers
> and, if found, write $domain to a file localDNS.
> If our DNS servers are not found in the grep I'll write $domain to a file
> remoteDNS.
> For a domains in remoteDNS, I'll establish the actual DNS server from the
> output of the dig command and specify it as a variable $dnsserver.
> $dnsserver will be used in further DNS commands of the form:

> the results will be written to the remoteDNS file.

> etc etc


 
 
 

differentiate between standard output and standard error?

Post by Chris F.A. Johnso » Sat, 22 Dec 2001 06:41:33



> I've just read about file descriptors
> you can direct standard outout (1) with >
> you can direct standard error (2) with 2>

> so I could dig $domain NS > tmp 2> failure

> tmp would then only contain the standard output of the dig(?) and I could
> then grep tmp for the name servers. Tmp would be written over on each loop
> (could I say on each iteration?)

If you don't want to overwrite tmp or failure with each iteration, use:
        >> tmp
        2>> failure

This will append the message to the file rather than overwriting it.

Also, you can detect the success or failure of a command:

        command && echo SUCCESS || echo failed
or:
        if command
        then
            echo SUCCESS
        else
            echo failed
        fi

For your script, you could try this for starters:

        info=`dig $domain NS || echo $domain >> failure`
        if [ -n $info ]
        then
           ## dig succeeded with output
           ## do your grep here
        else
           ## dig succeeded with no output
           echo $domain >> remoteDNS
        fi



> > Is there a way to differentiate between the output resulting from an error
> > generated by a command and the successful completion of a command (both
> > normally written to the terminal)  I've heard about sterr and stout and
> > think they are both written to the terminal.

> > The command (dig) that I want to use in a script could:
> > 1 successfully return results
> > 2 successfully fail to return results because no results are available
> > 3 fail altogether due to network problems / unavailability of remote
> servers
> > etc

> > I presume 1 + 2 are stout and 3 is sterr    - can BASH pickup this
> > difference?

> > Thanks,

> > Edward.

> > Background:

> > I'm planing a script that will use the dig command to check where a domain
> > name is delegated to.

> > for $domain from a list of domains, I'll run the command:
> > dig $domain NS.    (checks what DNS servers a domain is delegated to)

> > If the command fails (sterr) I'll write the domain to a failure file
> > If the command runs successfully, I'll grep the output for our DNS servers
> > and, if found, write $domain to a file localDNS.
> > If our DNS servers are not found in the grep I'll write $domain to a file
> > remoteDNS.
> > For a domains in remoteDNS, I'll establish the actual DNS server from the
> > output of the dig command and specify it as a variable $dnsserver.
> > $dnsserver will be used in further DNS commands of the form:

> > the results will be written to the remoteDNS file.

> > etc etc

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

differentiate between standard output and standard error?

Post by Edwar » Sat, 22 Dec 2001 07:21:22


Thanks for the advice Chris.




> > I've just read about file descriptors
> > you can direct standard outout (1) with >
> > you can direct standard error (2) with 2>

> > so I could dig $domain NS > tmp 2> failure

> > tmp would then only contain the standard output of the dig(?) and I
could
> > then grep tmp for the name servers. Tmp would be written over on each
loop
> > (could I say on each iteration?)

> If you don't want to overwrite tmp or failure with each iteration, use:
> >> tmp
> 2>> failure

> This will append the message to the file rather than overwriting it.

> Also, you can detect the success or failure of a command:

> command && echo SUCCESS || echo failed
> or:
> if command
> then
>     echo SUCCESS
> else
>     echo failed
> fi

> For your script, you could try this for starters:

> info=`dig $domain NS || echo $domain >> failure`
> if [ -n $info ]
> then
>    ## dig succeeded with output
>    ## do your grep here
> else
>    ## dig succeeded with no output
>    echo $domain >> remoteDNS
> fi



> > > Is there a way to differentiate between the output resulting from an
error
> > > generated by a command and the successful completion of a command
(both
> > > normally written to the terminal)  I've heard about sterr and stout
and
> > > think they are both written to the terminal.

> > > The command (dig) that I want to use in a script could:
> > > 1 successfully return results
> > > 2 successfully fail to return results because no results are available
> > > 3 fail altogether due to network problems / unavailability of remote
> > servers
> > > etc

> > > I presume 1 + 2 are stout and 3 is sterr    - can BASH pickup this
> > > difference?

> > > Thanks,

> > > Edward.

> > > Background:

> > > I'm planing a script that will use the dig command to check where a
domain
> > > name is delegated to.

> > > for $domain from a list of domains, I'll run the command:
> > > dig $domain NS.    (checks what DNS servers a domain is delegated to)

> > > If the command fails (sterr) I'll write the domain to a failure file
> > > If the command runs successfully, I'll grep the output for our DNS
servers
> > > and, if found, write $domain to a file localDNS.
> > > If our DNS servers are not found in the grep I'll write $domain to a
file
> > > remoteDNS.
> > > For a domains in remoteDNS, I'll establish the actual DNS server from
the
> > > output of the dig command and specify it as a variable $dnsserver.
> > > $dnsserver will be used in further DNS commands of the form:

> > > the results will be written to the remoteDNS file.

> > > etc etc

> --
>     Chris F.A. Johnson                        http://cfaj.freeshell.org
>     ===================================================================
>     My code (if any) in this post is copyright 2001, Chris F.A. Johnson
>     and may be copied under the terms of the GNU General Public License

 
 
 

1. redirecting standard output and standard error

Hi,

I want to call a command using C programmatically using execv, but I
also want to capture the standard output and standard error to files I
specify through command line args.  I want to use the file descriptors
0, 1, and 2 appropriately for this, and I understand I should probably
call a fork()
when calling the execv so my program can return successfully.  But
from there,
I'm not sure where to start.  I think I want to probably use dup2 so I
can redirect the descriptors to files I open.  Maybe create a pipe
from the child process to the parent process, where I pass through the
pipe are the results of the command call ??  Just need some guidance
and want to make sure I'm in the right direction.  Thanks.

2. SuSE 6.1 - Outdated libs?

3. How to redirect standard error to standard output (in csh)?

4. Hey! I discovered a new key!

5. LCSDNYR 2001 -> standards, standards, standards

6. setuid setgid problem

7. how to direct standard output and error output to the same file in tcsh

8. changing window managers (help a linux idiot)

9. standard out and standard error

10. standard out and standard error questions

11. capturing standard error info but not standard out

12. Redirecting standard output and error to a file

13. standard error output