Sponsored Content
Top Forums Shell Programming and Scripting How to redirect stderr for <file if file does not exist? Post 302912397 by achenle on Thursday 7th of August 2014 08:28:20 PM
Old 08-07-2014
In theory, you should be able to close stderr with

Code:
2>&-

But that doesn't seem permanent - tracing bash execution shows that stderr is saved off using dup2, then "restored" using another dup2 call after it's closed.

Maybe you should just start your shell with stderr redirected to /dev/null?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect stdout and stderr

How can I redirect and append stdout and stderr to a file when using cron? Here is my crontab file: */5 * * * * /dir/php /dir/process_fns.php >>& /dir/dump.txt Cron gives me an 'unexpected character found in line' when trying to add my crontab file. Regards, Zach Curtis POPULUS (8 Replies)
Discussion started by: zcurtis
8 Replies

2. Shell Programming and Scripting

how can i redirect stderr to file in Make?

Hello all im using tcsh shell on sun Solaris , using the Make utility for compilation i will like to be able to redirect the stderr to file , how can it be done ? (0 Replies)
Discussion started by: umen
0 Replies

3. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

4. UNIX for Dummies Questions & Answers

Redirect just stderr to a file with a timestamp

I'm using below command to redirect stderr to a file but I also want to add timestamp to stderr.out to find out the date / time the error occurred. ls -ltr 2>>/tmp/stderr.out Thanks (5 Replies)
Discussion started by: mbak
5 Replies

5. Shell Programming and Scripting

Redirect stdout/stderr to a file globally

Hi I am not if this is possible: is it possible in bach (or another shell) to redirect GLOBALLY the stdout/stderr channels to a file. So, if I have a script script.sh cmd1 cmd2 cmd3 I want all stdout/stderr goes to a file. I know I can do: ./script.sh 1>file 2>&1 OR ... (2 Replies)
Discussion started by: islegmar
2 Replies

6. Shell Programming and Scripting

redirect stdout and stderr to file wrong order problem with subshell

Hello I read a lot of post related to this topic, but nothing helped me. :mad: I'm running a ksh script with subshell what processing some ldap command. I need to check output for possible errors. #!/bin/ksh ... readinput < $QCHAT_INPUT |& while read -p line do echo $line ... (3 Replies)
Discussion started by: Osim
3 Replies

7. Shell Programming and Scripting

How to redirect stderr to a file as well

Hello everyone, I'm a nooby in Linux, and I need some help. I have a shell script like this: echo "Start of script" > ../My_Log_Dir/Script_Name.log .. cp ../My_DataIn/File.txt ../My_DataOut/ 2>> ../My_Log_Dir/Script_Name.log rc=$? .. echo "End of Script" >>... (5 Replies)
Discussion started by: H.Faria
5 Replies

8. Shell Programming and Scripting

ksh- redirect stderr to file and then modify the file

I have the following: remsh $host -n 2>>syslog_issue_list.txt grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log | awk /"$DATE1"/ | awk -vhost="$host" '!/remsh|telnetd/{print host "\n", $0 >> "syslog_issue_list.txt"}' I am creating a health script that has... (4 Replies)
Discussion started by: chipblah84
4 Replies

9. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.logBut during script execution I would like the output come back again to screen, how to do that? Thanks Lucas (4 Replies)
Discussion started by: Lord Spectre
4 Replies

10. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: Code: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.log But during script execution I would like the output come back again to screen, how to do that? Thanks Luc edit by bakunin: please use CODE-tags like the... (6 Replies)
Discussion started by: tmonk1
6 Replies
explain_dup2(3) 					     Library Functions Manual						   explain_dup2(3)

NAME
explain_dup2 - explain dup2(2) errors SYNOPSIS
#include <libexplain/dup2.h> const char *explain_dup2(int oldfd, int newfd); const char *explain_errno_dup2(int errnum, int oldfd, int newfd); void explain_message_dup2(char *message, int message_size, int oldfd, int newfd); void explain_message_errno_dup2(char *message, int message_size, int errnum, int oldfd, int newfd); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the dup2(2) system call. explain_dup2 const char *explain_dup2(int oldfd, int newfd); The explain_dup2 function is used to obtain an explanation of an error returned by the dup2(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (dup2(oldfd, newfd) < 0) { fprintf(stderr, "%s ", explain_dup2(oldfd, newfd)); exit(EXIT_FAILURE); } oldfd The original oldfd, exactly as passed to the dup2(2) system call. newfd The original newfd, exactly as passed to the dup2(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_errno_dup2 const char *explain_errno_dup2(int errnum, int oldfd, int newfd); The explain_errno_dup2 function is used to obtain an explanation of an error returned by the dup2(2) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (dup2(oldfd, newfd) < 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_dup2(err, oldfd, newfd)); exit(EXIT_FAILURE); } errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. oldfd The original oldfd, exactly as passed to the dup2(2) system call. newfd The original newfd, exactly as passed to the dup2(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_message_dup2 void explain_message_dup2(char *message, int message_size, int oldfd, int newfd); The explain_message_dup2 function may be used to obtain an explanation of an error returned by the dup2(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (dup2(oldfd, newfd) < 0) { char message[3000]; explain_message_dup2(message, sizeof(message), oldfd, newfd); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. oldfd The original oldfd, exactly as passed to the dup2(2) system call. newfd The original newfd, exactly as passed to the dup2(2) system call. explain_message_errno_dup2 void explain_message_errno_dup2(char *message, int message_size, int errnum, int oldfd, int newfd); The explain_message_errno_dup2 function may be used to obtain an explanation of an error returned by the dup2(2) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (dup2(oldfd, newfd) < 0) { int err = errno; char message[3000]; explain_message_errno_dup2(message, sizeof(message), err, oldfd, newfd); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. oldfd The original oldfd, exactly as passed to the dup2(2) system call. newfd The original newfd, exactly as passed to the dup2(2) system call. SEE ALSO
dup2(2) duplicate a file descriptor explain_dup2_or_die(3) duplicate a file descriptor and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_dup2(3)
All times are GMT -4. The time now is 08:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy