How can I use logger in a shell script?
If I run
ntpd -qnp pool.ntp.org 2> >(logger)
in console, it works as expected.
If I put it into a shell script
#!/bin/sh
ntpd -qnp pool.ntp.org 2> >(logger)
I get following error:
line 2: syntax error: unexpected redirection
exited with code 2
What is wrong in the shell script?
The shebang is correct as both #!/bin/sh and #!/bin/bash can be used*, however, IIRC, you should use pipes not redirect for logger as it's a program not a file.
Try this script:
*If you use #!/bin/sh for your scripts, they are supposed to be posix compliant, ie run well on solaris and AIX. If ever you use some bash specific features, #!/bin/bash have to be used because there's no more portability.
What's wrong is that you're using bash-ism's while your hash-bang is saying
/bin/sh
. Change it to#! /bin/bash
and it will work.I actually had to check what the line
ntpd -qnp pool.ntp.org 2> >(logger)
does:ntpd -qnp pool.ntp.org
2>
to ... where exactly? Well....>(logger)
/dev/fd/pipe100
logger
reading its STDIN from that pipe/dev/fd/pipe100
into the commandntpd -qnp pool.ntp.org 2> /dev/fd/pipe100
logger
reads from that pipe:logger < /dev/fd/pipe100
The above should work nicely in bash.
However, I tried to run it using "sh" explicitly, which yields:
So, as the other commenters noted, you are probably not using the bash here.
Indeed, Busybox uses the Almquist Shell by default.
Most likely you have a different shell with /bin/sh than your default shell (probably bash)
To check try to run the script with /bin/sh ./yourscript or put the same shell as $SHELL into your script
Which shell is used for /bin/sh is probably distribution dependent plus if you are on Solaris, it is something completely different.