[BASH] redirect standard error and use it inside

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [BASH] redirect standard error and use it inside
# 1  
Old 02-29-2008
[BASH] redirect standard error and use it inside

Hi all,

Maybe my question is too simple but till now i couldn't figure about a solution Smilie

I have a bash script scheduled in cron:
<cron time parameters> my_script.sh > result.log 2>&1

By this way i can have standard output and standard error in my result.log file

Now i want my script to alert me when some critical errors occurs.
For example, if i want to do a check like that:
ls $dir
if [ $? -ne 0 ]
echo "ALERT ALERT ALERT" >>fileAlert.log

But as i redirected the stderror i can't catch it enymore with $?, isn't it?

When i launch the script, by commmand line:
my_script.sh > results.log 2>&1

i get the error:
"Ambiguous output redirect"

I can do something about that? Or maybe i miss something else?

For sure i can use AWK and similiar to parse output of each test in the script, but i would like to use the exit status of each command.
Is it possible??

thanks in advance! Smilie
# 2  
Old 02-29-2008
In my testing:

myscript contains:
ls -al bonehead
touch tmptest

Issuing the command:
./myscript > temp2test.log 2>&1
This writes the error for the nonexistent bonehead file and touches the tmptest file but returns 0 for echo $?

Changing myscript to contain an exit status at the end:
ls -al bonehead
touch tmptest
exit 231

This writes the error for the nonexistent bonehead file and touches the tmptest file, but returns 231 for echo $?

So, you can track the conditions of each command you need error notification on, and assign a "meaningful" exit status to a variable, then at the end of your script, exit "$somevariablename".

You could combine multiple error status values to the same variable for exit status that can be grep'd for major issue notification...

So exit status will need to be handled in the script somehow (as it really should be).

<edit> OR, just grep yer log file for the error messages you expect to receive notification on.
# 3  
Old 03-03-2008
Thank you Flying_Meat Smilie

I'm trying to do like you suggest in my scripts...

But, there isn't in your opinion a way to simply duplicate the standard error?
To let have it "as a copy" in the logfile when the sript is lauched, and still can use it inside the script.
Something like the tee command do for standard output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Redirect Standard Error to /dev/null is not working.

Hello. When I run a .ksh that contains the command below, and there is no file available in the source location the "FILE_NAME_*.CSV not found" error is still being displayed. FILEN=$(ssh ${SOURCE_SERV} "cd ${SOURCE_LOCATION} ;ls ${FILES}") 2> /dev/null. This is interfering with the rest... (4 Replies)
Discussion started by: jimbojames
4 Replies

2. Shell Programming and Scripting

How redirect standard output to a file

Hi guys, i have a script named purgeErrors.ksh, when i execute this script i need to redirect the output to a log file in the same directory, how can i do that ?? -- Aditya (5 Replies)
Discussion started by: chaditya
5 Replies

3. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

4. Shell Programming and Scripting

Redirect standard error to input of other process, 2| ?

Hello, I would like to know if there is a shell in which operations such as 2| (redirect standard error of one process to the standard input of another one) exist? I know it is possible to do it in bash with things like: (process 2>&1) | other_process but I find it a bit intricate when... (3 Replies)
Discussion started by: chlorine
3 Replies

5. Shell Programming and Scripting

loop logic inside of an inline redirect?

i need to log the feedback from the ftp server as i'm performing some deletes. the only way i know of to do this is with the inline redirect << EOF ... but from there to the closing EOF, it's like i'm at the ftp command prompt, so I don't know how to have ksh script logic in there I have an... (3 Replies)
Discussion started by: tlavoie
3 Replies

6. Shell Programming and Scripting

standard error to standard out question

Hi there how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

7. Programming

Redirect Standard Output Multi-Process

Hi, I'm trying to compile the following code: /************** Begin <test.c> ***************/ /* * Compiled with: gcc -Wall -o test test.c */ #include <stdio.h> #include <unistd.h> int main(void) { printf("I'm process %d, son of %d \n", getpid(), getppid()); ... (5 Replies)
Discussion started by: djodjo
5 Replies

8. Shell Programming and Scripting

redirect only the standard error output to mail

I'm writing a script using file descriptor 2 (std error) to send an email only if the command fails or errors out but the script always emails me irrepective of whether it fails or not. It will not email the /tmp/check.error file output if doesn't error out just the mail with the subject "Cannot... (3 Replies)
Discussion started by: barkath
3 Replies

9. UNIX for Dummies Questions & Answers

Question from a newbie. How to redirect standard output

I have a program that is sending error text to the console and I need to redirect that output to a log file. I'm brand new to Unix and don't know how to do this. Any direction would be greatly appreciated. (1 Reply)
Discussion started by: ndemos
1 Replies

10. UNIX for Dummies Questions & Answers

redirect standard error into log file

Hi, I am new in shell scripting. Can anyone point out what wrong of below script. If I want the error output to "sqlerror.log" and database pool data output to "bulk_main.dat". Right now, the below script, if successful execute, the data will output to bulk_main.dat && sqlerror.log both... (7 Replies)
Discussion started by: epall
7 Replies
Login or Register to Ask a Question