1824996 Members
2159 Online
109678 Solutions
New Discussion юеВ

Re: Echo to StdErr

 
SOLVED
Go to solution
Seth Hollist
Occasional Advisor

Echo to StdErr

How could I use and echo or print statement from within a shell script to send information to Standard Error instead of Standard Out?

Thanks.
There's always room for improvment
13 REPLIES 13
Sridhar Bhaskarla
Honored Contributor

Re: Echo to StdErr

Seth,

How about

echo "your_message" 1>&2

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
linuxfan
Honored Contributor

Re: Echo to StdErr

Hi Seth,

By default both STDOUT and STDERR are displayed on your terminal.

Within a script if you want to send the display of STDOUT to STDERR you could do something like

echo "message" 2>err_log 1>&2

where your STDERR is being sent to err_log and you are forcing STDOUT to be redirected to STDERR (which is basically err_log)

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Sridhar Bhaskarla
Honored Contributor

Re: Echo to StdErr

Ramesh,

Just curious - what's the difference in the outputs of the conventional of redirection like

echo "my process" > errlog 2>&1

and the one you stated?

I am very much interested to know the case where Seth wanted to use stdErr.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
linuxfan
Honored Contributor

Re: Echo to StdErr

Hi Sridhar,

Actually AFAIK if you redirect STDOUT and STDERR to the same file, then it doesn't matter if you are using 2>&1 or 1>&2

Also I don't believe there is any difference in
echo "messages" > error.log 2>&1 and
ehco "messages" 2>error.log 1>&2

Since both the STDOUT and STDERR are getting redirected to the same file(error.log).

The only reason i specified 2>error_log is because sometimes i want STDOUT and STDERR to go to different files.

-Regards
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Seth Hollist
Occasional Advisor

Re: Echo to StdErr

The idea is to send STDOUT to a file and to send STDERR to the terminal or display.

echo my_message 1>&2 seems to work fine for what I wanted, but it would be better if I could prevent it from going to STDOUT as to keep it from wrighting to the log file as well.
There's always room for improvment
Sridhar Bhaskarla
Honored Contributor

Re: Echo to StdErr

In fact, it doesn't really matter Seth. By default StdOut and StdErr will send the output to the terminal.

So, it is just the same if you do
echo "message" 2>&1 or simply
echo "message".

After understanding your task, I feel you can do this way.

some_commands > /tmp/yourlogfile

You are deliberately not specifying the StdErr redirection here so it will by default output to the terminal. However, the StdOut will go to yourlogfile.

What do you say?

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
linuxfan
Honored Contributor

Re: Echo to StdErr

Hi Seth,

If you do
echo "messages" 1>&2
both STDOUT and STDERR will be displayed on your screen

echo "messages" 2>error_log
will display STDOUT to screen and STDERR to the error_log file

echo "messages" >error_log
will display STDERR to the screen and STDOUT to the error_log file

echo "messages" >error_log 2>&1
will redirect both STDOUT and STDERR to the file error_log

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Seth Hollist
Occasional Advisor

Re: Echo to StdErr

I guess I should be more specific...

I've got a program that populates files with certain information. Within that program it calles another script

Other_script > log_file

Within the other script I have

echo my_message 1>&2

That way I see my_message as if it were eched to the terminal from the original scrip, even though it's comeing from the Other_scrip and getting into the log file as well.
There's always room for improvment
S.C. Fun
Advisor

Re: Echo to StdErr

Hi seth,
From the above, I perceived this is what u mean:
1.The script Other_script will produce stderr.
2.You need to redirect this stderr to log_file.
3.You also want to see this stderr on terminal when u run the script containing Other_script.
4.Stdout will go to log_file and terminal.

If the above is correct, then try this in your script:

other_script 2>&1|tee log_file

Rgds,
S.C. Fun
Alan Riggs
Honored Contributor
Solution

Re: Echo to StdErr

If you script in posix or ksh then use the print command instead of echo. It has a number of useful features, among them the -u flag which allows you to specify which file descriptor (I/O stream) you wish to print to. Since STDERR is defined as descriptor 2, simply use:

print -u 2 "whatever"
Seth Hollist
Occasional Advisor

Re: Echo to StdErr

S.C. Fun,

> 1.The script Other_script will produce stderr.

- Yes, That's bascially my whole question right there, how to make the script produce stderr.

> 2.You need to redirect this stderr to log_file.

- No, I'm already redirecting stdout to log_file. I don't need anything else going to the log_file, but I do want to be able to send other messages to the terminal through the use of stderr.

> 3.You also want to see this stderr on terminal when u run the script containing Other_script.

- Yes, stderr needs to only go to the termial.

> 4.Stdout will go to log_file and terminal.

- No... Stdout and only Stdout will go to the log_file.
There's always room for improvment
Joseph C. Denman
Honored Contributor

Re: Echo to StdErr

WOW!! I'm still confused ***as always***

example, if you have a script.

#/sbin/sh #test.sh
echo "This is a test" >&2
##end of script

then run it as:

./test.sh > log_file

-This is a test- will be appear on the terminal, but will not go to the log_file. Only standard out will go to log_file. Is this what you are asking?


...jcd...
If I had only read the instructions first??
Seth Hollist
Occasional Advisor

Re: Echo to StdErr

Yeah, something like that, except that

echo "This is a test" >&2

will still send something to the log file because all your doing is telling stdout to also go to stderr.

The fallowing answered my question and works great. Thanks.

print -u 2 "whatever"
There's always room for improvment