简体   繁体   中英

embedded shell does not support redirection: exec 2> >(logger -t myscript)

I'm trying to run the command from this question :

exec 2> >(logger -t myscript)

It works great on my desktop linux system, however, on my embedded linux device the same command presents the following error:

-sh: syntax error near unexpected token `>'

So I'm guessing my shell doesn't like part of the command syntax - most likely this portion:

exec 2> > (logger -t myscript)

In fact, while I understand that the 2> is redirecting stderr I don't actually understand the syntax of the second > character in this case, is it another way of representing a pipe?

If I can understand what it is doing then perhaps I can modify my command to work with my limited shell on the embedded linux device.

The syntax in question only works with bash (or other shells with ksh extensions). In the error

-sh: syntax error near unexpected token `>'

...you're trying to use that syntax with /bin/sh .

Be sure your script starts with #!/bin/bash , and that you invoke it with bash yourscript rather than sh yourscript .


A bit more explanation:

  • >(foo) gets replaced with a filename (of the form /dev/fd/## if supported, or a named pipe otherwise) which receives output from a process named foo . This is the part that requires bash or ksh extensions.
  • exec <redirection> applies a redirection to the current shell process (thus, exec 2>stderr.log redirects all stderr from the current command and its children to the file stderr.log ).

Thus, exec 2> >(foo) modifies the stderr file descriptor (of your current shell session) to go to the stdin of command foo ; in this case, foo is logger -t myscript , thus sending the process's stderr to syslog.


To perform the same operation on a more limited (but still POSIX-compliant) shell:

# note: if any security concerns exist, put the FIFO in a directory
#       created by mktemp -d rather than hardcoding its name

mkfifo /tmp/log.fifo                 # create the FIFO
logger -t myscript </tmp/log.fifo &  # start the reader in the background first!
exec 2>/tmp/log.fifo                 # then start writing
rm -f /tmp/log.fifo                  # safe to delete at this point

>( command ) is a bash construct called "process substitution". man bash says:

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files.

Your shell doesn't seem to be bash, anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM