Home > ksh redirect > redirect standard error to standard out ksh

Redirect Standard Error To Standard Out Ksh

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business ksh redirect stderr and stdout to file Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation ksh redirect output to file Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.3 million programmers, just like ksh redirect stderr to file you, helping each other. Join them; it only takes a minute: Sign up Copy stderr and stdout to a file as well as the screen in ksh up vote 2 down vote favorite 2 I'm looking for

Ksh Redirect Stderr And Stdout To /dev/null

a solution (similar to the bash code below) to copy both stdout and stderr to a file in addition to the screen within ksh on Solaris. The following code works great in the bash shell: #!/usr/bin/bash # Clear the logfile >logfile.txt # Redirect all script output to a logfile as well as their normal locations exec > >(tee -a logfile.txt) exec 2> >(tee -a logfile.txt >&2) date ls -l /non-existent/path For some reason this is throwing ksh redirect stderr to dev null a syntax error on Solaris. I assume it's because I can't do process substitution, and I've seen some posts suggesting the use of mkfifo, but I've yet to come up with a working solution. Does anyone know of a way that all output can be redirected to a file in addition to the default locations? shell ksh io-redirection share|improve this question asked Aug 22 '12 at 14:36 HuggieRich 13114 add a comment| 3 Answers 3 active oldest votes up vote 2 down vote accepted Which version of ksh are you using? The >() is not supported in ksh88, but is supported in ksh93 - the bash code should work unchanged (aside from the #! line) on ksh93. If you are stuck with ksh88 (poor thing!) then you can emulate the bash/ksh93 behaviour using a named pipe: #!/bin/ksh # Clear the logfile >logfile.txt pipe1="/tmp/mypipe1.$$" pipe2="/tmp/mypipe2.$$" trap 'rm "$pipe1" "$pipe2"' EXIT mkfifo "$pipe1" mkfifo "$pipe2" tee -a logfile.txt < "$pipe1" & tee -a logfile.txt >&2 < "$pipe2" & # Redirect all script output to a logfile as well as their normal locations exec >"$pipe1" exec 2>"$pipe2" date ls -l /non-existent/path The above is a second version to enable stderr to be redirected to a different file. share|improve this answer edited Aug 23 '12 at 14:22 answered Aug 23 '12 at 10:23 cdarke 19.5k23143 this

ls | wc -l But bourne-shell derivatives give you even more power than that. Most properly written programs output in one of two ways. Progress messages go to stdout, error messages go to stderr Data goes to stdout, error AND progress messsages go to stderr If http://www.bolthole.com/solaris/ksh-redirection.html you know which of the categories your utilities fall into, you can do interesting things. Redirection An uncommon program to use for this example is the "fuser" program under solaris. it gives you a long listing of what processes are using http://superuser.com/questions/195560/re-redirect-stderr-in-k-sh a particular file. For example: $ fuser /bin/sh /bin/sh: 13067tm 21262tm If you wanted to see just the processes using that file, you might initially groan and wonder how best to parse it with awk or something. However, fuser actually splits up ksh redirect the data for you already. It puts the stuff you may not care about on stderr, and the meaty 'data' on stdout. So if you throw away stderr, with the '2>' special redirect, you get $ fuser /bin/sh 2>/dev/null 13067 21262 which is then trivially usable. Unfortunately, not all programs are that straightforward :-) However, it is good to be aware of these things, and also of status returns. The 'grep' command actually returns a status based on whether it found a line. The ksh redirect stderr status of the last command is stored in the '$?' variable. So if all you care about is, "is 'biggles' in /etc/hosts?" you can do the following: grep biggles /etc/hosts >/dev/null if [[ $? -eq 0 ]] ; then echo YES else echo NO fi As usual, there are lots of other ways to accomplish this task, even using the same 'grep' command. However, this method has the advantage that it does not waste OS cycles with a temp file, nor does it waste memory with a potentially very long variable. (If you were looking for something that could potentially match hundreds of lines, then var=`grep something /file/name` could get very long) Inline redirection You have seen redirection TO a file. But you can also redirect input, from a file. For programs that can take data in stdin, this is useful. The 'wc' can take a filename as an argument, or use stdin. So all the following are roughly equivalent in result, although internally, different things happen: wc -l /etc/hosts wc -l < /etc/hosts cat /etc/hosts | wc -l Additionally, if there are a some fixed lines you want to use, and you do not want to bother making a temporary file, you can pretend part of your script is a separate file!. This is done with the special '<<' redirect operator. command << EOF means, "run 'command', but make its stdin come from this file right here, until you see the string 'EOF'" EOF is the traditional string

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Super User Questions Tags Users Badges Unanswered Ask Question _ Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Re-redirect STDERR in K/SH? up vote 1 down vote favorite Once STDERR is redirected, for example to a file, how can it be redirected back to the original file descriptor 2? #!/bin/sh exec 2>/tmp/err print -u2 'log errors to file' exec 2>&2 (how do we do this???) print -u2 'log errors to console' Context This redirection solution should work in a cron job. For example, if cron executes an external script, that script will redirect stdout and stderr to relevant log and error files, respectively. However, if an execution in the script fails or something needs to be reported to the job owner, I need to output the error messages so cron will receive them and send mail. shell shell-script ksh share|improve this question edited Oct 4 '10 at 7:07 asked Oct 4 '10 at 5:34 Clint Pachl 79858 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote accepted Try: #!/usr/bin/env ksh exec 2>ksh_err.txt print -u2 'log errors to file' exec 2>`tty` print -u2 'log errors to console' Edit 1: #!/usr/bin/env ksh exec 8>err.txt print -u8 'log errors to file' print -u2 'log errors to console' Edit 2: #!/usr/bin/env ksh exec 3>&2 exec 2>err.txt print -u2 'log errors to file' exec 2>&3 print -u2 'log errors to console' share|improve this answer edited Oct 4 '10 at 10:05 answered Oct 4 '10 at 6:49 Casual Coder 2,7431712 This would work if my situation was different. I just updated my question. –Clint Pachl Oct 4 '10 at 7:08 I've updated my answer. –Casual Coder Oct 4 '10 at 8:20 That would work too, but just not in my case because I cannot control which file descriptor gets written to. The script in question running from cron contains dump(8). Dump by default writes status/progress to STDERR and dump data to STDOUT (pipe to SSH). I want to redirect dump's STDERR to a dumplog for the entire script because there are several invocations of dump throughout the script. However, if the script must report other error

Related content

redirect standard error ksh
Redirect Standard Error Ksh p do I redirect error messages Note The following information pertains primarily to Bourne-like shells such as sh ksh and bash To redirect standard error messages to a file enter command file Replace p Ksh Redirect Output To File p command with the command you want to execute and file with the file to ksh redirect stderr and stdout to dev null which you want to direct the errors for example gunzip errors If you wish to suppress error messages enter p Ksh Redirect Stderr To Dev Null p command - If you wish to redirect

standard error redirect ksh
Standard Error Redirect Ksh p do I redirect error messages Note The following information pertains primarily to Bourne-like shells such as sh ksh and ksh redirect output to file bash To redirect standard error messages to a file enter command ksh redirect stderr and stdout to dev null file Replace command with the command you want to execute and file with the file to p Ksh Redirect Stderr To Dev Null p which you want to direct the errors for example gunzip errors If you wish to suppress error messages enter command - If you wish to redirect p Ksh

unix ksh error redirection
Unix Ksh Error Redirection p do I redirect error messages Note The following information pertains primarily to Bourne-like shells such as sh ksh and bash To redirect standard error messages to a file ksh redirect stderr and stdout to file enter command file Replace command with the command you want to execute p Ksh Redirect Stderr To File p and file with the file to which you want to direct the errors for example gunzip errors p Ksh Redirect Output To File p If you wish to suppress error messages enter command - If you wish to redirect error messages