help with unix redirecting to stderror


 
Thread Tools Search this Thread
Top Forums Programming help with unix redirecting to stderror
# 1  
Old 03-12-2011
help with unix redirecting to stderror

when writing a script, when redirececting a message to stderror, how do i do it?
i can have
echo "message" 2> error.log

is it neccessary to do it while creating a file, if not how do i send it to stderror and have it displayed on the screen at same time, and not have to create a file.
# 2  
Old 03-12-2011
Quote:
i can have
echo "message" 2> error.log
You could, but it wouldn't do anything, just print 'message' to standard output and create an empty file 'error.log'.

What are you actually trying to do?
# 3  
Old 03-12-2011
redirect it to stderr
# 4  
Old 03-13-2011
Quote:
Originally Posted by omega666
when writing a script, when redirececting a message to stderror, how do i do it?
i can have
echo "message" 2> error.log

is it neccessary to do it while creating a file, if not how do i send it to stderror and have it displayed on the screen at same time, and not have to create a file.
I'm going to read between the lines and assume that you want to output to stderr within a script so that if you run the script with stderr redirected to a file, that output goes to the file.

The syntax is
Code:
echo "message" 1>&2

Generally
Code:
x>&y

where "x" and "y" are integers means to redirect file-descriptor "x" to the same place "y" is going. FD 1 is stdout (which "echo" writes to) and FD 2 is stderr, so
Code:
1>&2

(or just
Code:
>&2

which implies redirecting stdout) means redirect stdout to whatever stderr is connected to.
# 5  
Old 03-13-2011
then what does this do?

echo "message" > /dev/stderr
# 6  
Old 03-13-2011
It redirects standard output(FD 1, the default output to redirect) to standard error(FD 2).

That's not the standard way to do it by the way, as some systems don't even have a "/dev/stderr" thing (which is usually just a symbolic link anyway). Best way to do it is echo message >&2 This takes the default output FD, 1, and redirects it into standard error, i.e. FD 2.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

2. Shell Programming and Scripting

SFTP stderror not able to trap

(7 Replies)
Discussion started by: krupasindhu18
7 Replies

3. Shell Programming and Scripting

Redirect stdout and stderror in child process

I have a problem when i try to create a log file from a daemon process using shell scripting in ubuntu 12. Ultimatly what i want to achieve is run a java/jar file from a script. After scourging the internet i found several solutions to do this, the one i choose is to create a startup script that... (4 Replies)
Discussion started by: Narev
4 Replies

4. Shell Programming and Scripting

Automatically send stdout and stderror to a file as well as to the screen, but without using tee

Hi, I've been using the following commands in my automated scripts, to ensure that all text output is sent to a log file instead of to the screen: exec 1>>$SCRIPT_LOG_FILE exec 2>>$SCRIPT_LOG_FILE However, I've now discovered that the system used for automating the script executions... (4 Replies)
Discussion started by: confusedAdmin
4 Replies

5. Shell Programming and Scripting

Reading UNIX commands from file and redirecting output to a file

Hi All I have written the following script: #!/bin/ksh while read cmdline do echo `$cmdline` pid="$cmdline" done<commands.txt =========== commands.txt contains: ps -ef | grep abc | grep xyz |awk '{print $2}; My objective is to store the o/p of the command in a variable and do... (8 Replies)
Discussion started by: rahulparo
8 Replies

6. Shell Programming and Scripting

redirecting help

I am trying to create the file and redirect the output in the same command line which is line 4 in the below program. #!/bin/bash read -p "Enter File Name:" value1 echo "Your File Name is $value1" sed 's/abcd/'$value1'/g' abcd_calls > $value1_calls This is the error it generates ... (3 Replies)
Discussion started by: learnbash
3 Replies

7. Programming

Redirecting

How to redirect the contents of a file to a command? The contents of the file are the arguments necessary for the command. thx in advance. bye svh (5 Replies)
Discussion started by: svh
5 Replies

8. UNIX for Dummies Questions & Answers

Redirecting UNIX Print Output

I am a new user to UNIX. We are currently running an application called DISC (www.disclink.com) on UNIX, and I am trying to figure out how to redirect print output to a PC printer. There is one little problem: DISC apparently has its own OS that is sitting on top of the UNIX OS, so you cannot... (1 Reply)
Discussion started by: BlueSpider
1 Replies
Login or Register to Ask a Question