Writing to the stderr of a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing to the stderr of a script
# 1  
Old 02-28-2010
Writing to the stderr of a script

I have Mac OS X, and I wrote a bash shell script that does some encryption of files. When the user of the script does something wrong I want it to send an error message to the stderror of the script. Here's the code snippet I have:

Code:
printf "$1: File not found\nNote: This script will only work on files.  It will not take bare strings.\n" > $0 1>&2;

$0 is (according to my sources) supposed to be the name of the script, but for some reason, for me it's always "bash". I think this may be a Mac thing. When I ran the script and sent its stderr to a file, I found that the file was empty afterward. What's happening is pretty obvious: the script sends the standard output of printf to the standard error of the bash shell, rendering it practically inaccessible.

So my question is how can I access the name of the process for the script, seeing as $0 doesn't work?

Last edited by Ultrix; 02-28-2010 at 06:37 PM..
# 2  
Old 02-28-2010
output to stderr
>&2 printf

oops i was answering the thread title.
how are you calling the script?
# 3  
Old 02-28-2010
Quote:
Originally Posted by bigearsbilly
>&2 printf
That's not the problem. It's printing the error message just fine, but it's not redirecting the output to the stderr of the script, just to the stderr of bash.
# 4  
Old 02-28-2010
Ok, one thing after the other...

$0 is the name of the program running. What's put there depends on how you call it. But you can't use that name in the sense of "redirect something to that program".

Redirections are done from left to right. So your line
Code:
printf "$1: File not found\nNote: This script will only work on files.  It will not take bare strings.\n" > $0 1>&2;

actually means "take the output of printf, redirect it to the file named in $0, and if there's still something on stdout redirect it to stderr"

What you want is
Code:
printf "$1: File not found\nNote: This script will only work on files.  It will not take bare strings.\n" 1>&2

Which means "take printf's output, and redirect it to stderr"
# 5  
Old 02-28-2010
no you are doing it wrong ultrix,
first you do:
> $0 (print output to the script!)
then over riding with >&2

order is important.
the canonical way is

>&2 printf thing > out.log
# 6  
Old 02-28-2010
Quote:
Originally Posted by pludi
Ok, one thing after the other...

$0 is the name of the program running. What's put there depends on how you call it. But you can't use that name in the sense of "redirect something to that program".

Redirections are done from left to right. So your line
Code:
printf "$1: File not found\nNote: This script will only work on files.  It will not take bare strings.\n" > $0 1>&2;

actually means "take the output of printf, redirect it to the file named in $0, and if there's still something on stdout redirect it to stderr"

What you want is
Code:
printf "$1: File not found\nNote: This script will only work on files.  It will not take bare strings.\n" 1>&2

Which means "take printf's output, and redirect it to stderr"
Yes, that works. It redirects to stderr. Thanks for the help.
# 7  
Old 03-01-2010
Code:
cmd  >> somestdout.redirect.file.append  # stderr not

cmd 2>> somestderr.redirect.file.append  # stdout not

cmd 2> somefile # overwrite somefile, only stderr

cmd > some.stdout  2>some.stderr   
cmd >> some.stdout  2>>some.stderr  

cmd >&2           # output redirect to the stderr

cmd >&2 | cmd2   # stderr also to the pipe
cmd 2>>err  |  cmd2  # only stdout to the pipe

# stdout redirect to the file and stderr redirect to the stdout = somefile
cmd  > somefile  2>&1   

# maybe never used: stderr redirect stdout and after that stdout redirect to 
# the somefile - stderr is not redirected to the somefile
cmd 2>&1 > somefile 

# after this all stderr has redirected to the file err.log, 
# very nice ex. in cgi using.
exec 2>>err.log 

cmd >/dev/null 2>&1   # output is not interesting

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. How to Post in the The UNIX and Linux Forums

Help with script writing?

I am new for script writing. I have a file named as name.dat and contain numbers with different rows and columns. I want to sum numbers at one row with that of at another row; like row1 + row101 + row 201, with corresponding columns. Any one can help me to write a script for this operation. ... (1 Reply)
Discussion started by: kumnegert
1 Replies

2. Shell Programming and Scripting

Capturing stderr in ksh script within a script

I have seen lots of entries on stderr and stdout, but I did not see a solution to my question. In running a script, I call another script. I want to capture output and error messages from the called script. I am able to redirect the stdout from the called script to my output file, but I don't... (2 Replies)
Discussion started by: timtoben
2 Replies

3. Shell Programming and Scripting

I need help writing this script

:wall: Can't seem to figure out how to fix this please help its not starting over like I would like it to When I enter in "Date" or "Time" nothing comes Also if you can tell me the commands for the other 3 stuff that would be much appreciated #!/bin/bash clear while ; do echo... (8 Replies)
Discussion started by: nowruzr
8 Replies

4. Shell Programming and Scripting

STDOUT and STDERR redirection within a script

Hello all, I have a for loop executing in a script that I want to redirect STDOUT to screen and to file, while directing STDERR to the bit bucket. Here is the general sentax of what I'm doing: for i in thingy do some_command ${i} done 1>&1 | tee ${LOGFILE} 2> /dev/null What I am... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

5. UNIX for Advanced & Expert Users

Truncation Occurs When Outputting Shell Script to stderr

Operating System: Solaris 10, Shell We are outputting the results of our scripts to the stderr file. However we have encountered a problem where some of the lines in the file are truncated. Is there a way to increase the terminal or column size within the script so that this does not... (4 Replies)
Discussion started by: fazzasx
4 Replies

6. Shell Programming and Scripting

Redirecting STDERR to a file from within a bash script

I am trying to redirect the output from stderr to a log file from within a bash script. the script is to long to add 2> $logfile to the end of each command. I have been trying to do it with the command exec 2> $logfile This mostly works. Unfortunately, when a read command requires that anything be... (5 Replies)
Discussion started by: vockleya
5 Replies

7. UNIX for Dummies Questions & Answers

Need help writing this script

Here is the script I am trying to write along with my answer I wrote. Please help me understand why it doesn't work. Create an executable script file called "newname" that will perform the followings: 1. Rename a file upon the user's request. If the file exists, prompt the user for... (1 Reply)
Discussion started by: wiggles
1 Replies

8. Shell Programming and Scripting

Writing the script

I whant write the script of monitoring my servers on night-active users. He must loging all user actions, when user login, when logout, when he do su etc. But i don't know with what to begin :( How command i can use? awk, sed, last And what logs i can use too? /var/log/messages, /var/log/secure... (1 Reply)
Discussion started by: jess_t03
1 Replies

9. Shell Programming and Scripting

need help writing a script

Hello everyone. Well, I will get right to the point. I am new to Perl and trying to learn it as much as I can. I have been assigned the task of writing a perl script to extract information from firewall logs. Like I said, I am new to Perl and I am having a tough time because I think what I am... (3 Replies)
Discussion started by: tarballed
3 Replies

10. Shell Programming and Scripting

stderr redirection in csh script

Man pages....look at the man pages. If you don't have them, you can find them on-line. Read them when you have nothing better to do. Find new commands and new ways of doing things. The answer: The only way to direct the standard output and standard error separately is by invoking... (0 Replies)
Discussion started by: thehoghunter
0 Replies
Login or Register to Ask a Question