Find the answer to your Linux question:
Results 1 to 5 of 5
I want to do the following redirectin inside a bash script : 1. all the stdout will be redirected to a /tmp/log 2. all the errors will go to the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28

    how to redirect inside bash script, BUT...


    I want to do the following redirectin inside a bash script :
    1. all the stdout will be redirected to a /tmp/log
    2. all the errors will go to the stderr and also to the /tmp/log.

    The following inside a bash script is not working, WHY and HOW to do it:

    #!/bin/bash
    exec 1>/tmp/log
    exec 2> > (tee -a /tmp/log)
    ....
    rest of the bash script.

  2. #2
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28

    Experts on bash Redirections?

    Anyone can help on that?

  3. #3
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85

  4. #4
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28

    Redirection issue

    I didn't find the answer to my question in the link(BASH Programming - Introduction HOW-TO: All about redirection).

    This link has a basic redirection example, and want I need it a bit complicated redirection issue.

  5. #5
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    I think what you'll have to do is create a child process running tee and use a named pipe to redirect stderr to it. That way stderr will print on the terminal and to the log.

    Code:
    #!/bin/bash
    npipe=/tmp/$$.tmp
    trap "rm -f $npipe" EXIT
    # Delete named pipe on script exit
    mknod $npipe p
    # Create the named pipe
    tee <$npipe -a /tmp/log &
    # Create child process running tee with stdin redirected to named pipe
    exec 1>/tmp/log
    # Redirect stdout to log
    exec 2>&-
    # Close stderr
    exec 2>$npipe
    # Open stderr on named pipe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •