Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 662679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:24:18+00:00 2026-05-13T23:24:18+00:00

I would like to create a pipe in a ksh script (using exec) that

  • 0

I would like to create a pipe in a ksh script (using exec) that pipe’s to a tee, and sends the output to a pipe.

Current:

#Redirect EVERYTHING
exec 3>&1 #Save STDOUT as 3
exec 4>&2 #Save STDERR as 4
exec 1>${Log} #Redirect STDOUT to a log
exec 2>&1 #Redirect STDERR to STDOUT

What’d I’d like to do (but I don’t have the syntax correct):

#Redirect EVERYTHING
exec 3>&1 #Save STDOUT as 3
exec 4>&2 #Save STDERR as 4
exec 1>tee -a ${Log} >&3  #Redirect STDOUT to a log
exec 2>&1 #Redirect STDERR to STDOUT

How can I create this pipe?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:24:19+00:00Added an answer on May 13, 2026 at 11:24 pm

    Here’s a solution I use. It works under ksh on my Mac. It’s nicely encapsulated into start_logging() and stop_logging() functions to make life easy.

    The code looks like this in practice:

    # Optional:
    #   Set the name and location of the log file.
    #   OUTPUT_LOG=output.log    # default
    #   Set the name and location of the named pipe used.
    #   OUTPUT_PIPE=output.pipe  # default
    
    start_logging
    # Default is to append to an existing log file.
    # start_logging delete_existing_logfile
    echo "This is on standard out"
    echo "This is on standard err" >&2
    stop_logging
    

    Here is the whole file. The start and stop functions along with the example above are all at the bottom of the file. To make it easier to use, just put the start and stop functions in their own file and source them in the scripts where you need the logging.

    #!/bin/sh
    
    # Author: Harvey Chapman <hchapman _AT_ 3gfp.com>
    # Description: POSIX shell functions that can be used with tee to simultaneously put
    #              stderr and stdout to both a file and stdout
    #
    # Based on:
    #    Re: How to redirect stderr and stdout to a file plus display at the same time
    #    http://www.travishartwell.net/blog/2006/08/19_2220
    
    #
    # Original example function from Travis Hartwell's blog.
    # Note: I've made minor changes to it.
    example()
    {
      OUTPUT_LOG=output.log
      OUTPUT_PIPE=output.pipe
    
      # This should really be -p to test that it's a pipe.
      if [ ! -e $OUTPUT_PIPE ]; then
          mkfifo $OUTPUT_PIPE
      fi
    
      # This should really be -f to test that it's a regular file.
      if [ -e $OUTPUT_LOG ]; then
          rm $OUTPUT_LOG
      fi
    
      exec 3>&1 4>&2
      tee $OUTPUT_LOG < $OUTPUT_PIPE >&3 &
      tpid=$!
      exec > $OUTPUT_PIPE 2>&1
    
      echo "This is on standard out"
      echo "This is on standard err" >&2
    
      exec 1>&3 3>&- 2>&4 4>&-
      wait $tpid
    
      rm $OUTPUT_PIPE
    }
    
    # A slightly reduced version of example()
    example2()
    {
      OUTPUT_LOG=output.log
      OUTPUT_PIPE=output.pipe
    
      rm -f $OUTPUT_PIPE
      mkfifo $OUTPUT_PIPE
      rm -f $OUTPUT_LOG
    
      tee $OUTPUT_LOG < $OUTPUT_PIPE &
      tpid=$!
    
      exec 3>&1 4>&2 >$OUTPUT_PIPE 2>&1
    
      echo "This is on standard out"
      echo "This is on standard err" >&2
    
      exec 1>&3 3>&- 2>&4 4>&-
      wait $tpid
      rm -f $OUTPUT_PIPE
    }
    
    #
    # Logging methods based on above. See the example below for how to use them.
    #
    
    # Usage: start_logging [delete_existing_logfile]
    start_logging()
    {
      # Check to see if OUTPUT_LOG and OUTPUT_PIPE need to be defined.
      if [ -z "$OUTPUT_LOG" ]; then
        OUTPUT_LOG=output.log
      fi
      if [ -z "$OUTPUT_PIPE" ]; then
        OUTPUT_PIPE=output.pipe
      fi
      # Make sure that we're not already logging.
      if [ -n "$OUTPUT_PID" ]; then
        echo "Logging already started!"
        return 1
      fi
    
      # Always remove the log and pipe first.
      rm -f $OUTPUT_PIPE
      # Delete the logfile first if told to.
      if [ "$1" = delete_existing_logfile ]; then
        rm -f $OUTPUT_LOG
      fi
    
      mkfifo $OUTPUT_PIPE
      tee -a $OUTPUT_LOG < $OUTPUT_PIPE &
      OUTPUT_PID=$!
    
      exec 3>&1 4>&2 >$OUTPUT_PIPE 2>&1
    }
    
    stop_logging()
    {
      # Make sure that we're currently logging.
      if [ -z "$OUTPUT_PID" ]; then
        echo "Logging not yet started!"
        return 1
      fi
      exec 1>&3 3>&- 2>&4 4>&-
      wait $OUTPUT_PID
      rm -f $OUTPUT_PIPE
      unset OUTPUT_PID
    }
    
    example3()
    {
      start_logging
      #start_logging delete_existing_logfile
      echo "This is on standard out"
      echo "This is on standard err" >&2
      stop_logging
    }
    
    #example
    #example2
    example3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like to create a stored procedure in MySQL that took a list
I would like to create events for certain resources that are used across various
I would like create a very simple Paint application using MAF on WPF. The
I would like create URL rewrite rule that will set default document for my
I would like create a custom DataRow that will have -let's say- a propery
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to create a database backed interactive AJAX webapp which has a
I would like to create an SSL connection for generic TCP communication. I think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.