Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In Bash you can redirect all future stdout output of the currently running script. For example with this script,

exec > >(logger -t my-awesome-script)
echo 1
echo 2
echo 3

This will end up in syslog:

Oct 26 01:03:16 mybox my-awesome-script[72754]: 1
Oct 26 01:03:16 mybox my-awesome-script[72754]: 2
Oct 26 01:03:16 mybox my-awesome-script[72754]: 3

But this is Bash-specific and the naked exec with redirection doesn't seem to work in Dash.

Syntax error: redirection unexpected

How can I make it work in Dash, or possibly in both shells?

share|improve this question
    
Could you clarify what you need exactly? You can redirect with > in dash. I realize you seem to be asking for something else but I can't quite tell what it is. –  terdon yesterday
    
@terdon I have expanded the explanation. –  Alex B yesterday

3 Answers 3

up vote 3 down vote accepted

You can just do:

{ commands
....
} | logger -t my_awesome_script

You can do that with any shell.

If you don't like the way it looks, maybe make the script wrap itself in a function.

#!/bin/sh
run() if     [ "$run" != "$$" ]
      then   run=$$ exec "$0" "$@" |
             logger -t my-awesome-script
      fi
#script-body
run || do stuff
share|improve this answer

I don't think this is possible in dash. As far as I can tell from its man page, it has no support for process substitution.

As a workaround, you could try what mikserv suggested, or you can redirect everything to a file, and then after your script is finished (presumably this is in a script), add that file's contents to logger:

$ exec > ~/foo/foo.txt
$ ls
$ echo something
$ cat foo/foo.txt | sudo logger -t my-awesome-script
share|improve this answer
    
In fact, process substitution - or what other shells call process substitution - is easier in dash then other shells. Process substitution just amounts to an argument that points to a /dev/fd/[num] link to an anonymous pipe. dash does here-documents with anonymous pipes rather than genning temp files as most other shells do. So cat /dev/fd/3 3<<HEREDOC\n$(get output)\nHEREDOC\n is not only functionally equivalent, you even get to name the fd yourself. Still, your point is well made about going the other way - you need to open a new fd with exec and background a process that reads it. –  mikeserv yesterday
2  
@mikeserv: In what sense is cat /dev/fd/3 3<<HEREDOC\n$(get output)\nHEREDOC\n "easier" than cat <(get output)? –  ruakh yesterday
    
@mikeserv: It involves plenty of rules to remember; perhaps you have simply grown so used to them that you don't notice. –  ruakh 19 hours ago
    
@ruakh - well, sure. < > shell redirections. basically if you just do two of those you can then pile on the following lines too. But yeah, you have a point - I like here docs. Still, as many things as might require remembering, it's easier when they work universally I think. Then again a lot of people don't have much use for other shells and so it makes no difference to them. I'm just not among them. –  mikeserv 16 hours ago
    
@mikeserv: Not just heredocs, but also /dev/fd/3 (in that precise form), and the details of what happens to whitespace . . . and for that matter, the fact that this whole approach works at all in Dash, when it doesn't work in other shells that have all of the components, means that the overall approach is a special rule to remember. (This reminds me of attempts to create a simplified English with less vocabulary; they cut out words like persist, but they ignore just-as-difficult idioms like keep on.) –  ruakh 15 hours ago

Process substitution is easily simulated with named pipes.

mkfifo logger_input
logger -t my_awesome_script < logger_input &
exec > logger_input
echo 1
echo 2
echo 3

In fact, named pipes are one of the mechanisms (the other being /dev/fd) with which process substitution can be implemented in bash.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.