Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash' network redirection preferred over nc #90

Closed
bdlow opened this issue Apr 28, 2022 · 4 comments
Closed

bash' network redirection preferred over nc #90

bdlow opened this issue Apr 28, 2022 · 4 comments

Comments

@bdlow
Copy link

bdlow commented Apr 28, 2022

The method provided in the AnyBar README, using nc, has limitations when the process feeding nc's stdin is a little tardy: with the -w0 argument, nc will not wait for input and may exit prematurely.

For example, this silently doesn't work:

( sleep 1; echo -n "black" ) | nc -4u -w0 localhost 1738

A workaround would be to use some arbitrarily high value for the wait argument, but fundamentally nc doesn't provide a means to associate stdin closing and closing the UDP "connection" (put another way, it doesn't have a "write only" mode). (at least one implementation of nc does provide a -c option that will wait for stdin to close, but it's not common)

BASH provides a very useful network redirection feature, this avoids the issue and also has fewer (zero!) dependancies:

( sleep 1; echo -n "black" ) > /dev/udp/localhost/1738

Suggest replacing the examples in the README with the BASH invocations.

@tonsky
Copy link
Owner

tonsky commented Apr 28, 2022

[~] ( sleep 1; echo -n "red" ) > /dev/udp/localhost/1738
zsh: no such file or directory: /dev/udp/localhost/1738

Doesn’t seem to work on macOS

@tonsky
Copy link
Owner

tonsky commented Apr 28, 2022

Seems to be disabled on Debian too https://unix.stackexchange.com/a/24095

@bdlow
Copy link
Author

bdlow commented Apr 28, 2022

macOS' default shell is zsh; zsh doesn't do UDP network redirection (it does do TCP via the zsh/net/tcp module). bash is installed, but just not the default. Most Linux distros, and Windows WSL, use bash by default; Debian initially explicitly disabled this feature, but stopped doing that ca. 2009: https://sources.debian.org/src/bash/5.0-4/debian/changelog/#L611-L619.

Perhaps the README should be, to cater for all platforms:

bash -c 'echo -n "black" > /dev/udp/localhost/1738'

And the Alternative clients > Bash alias:

function anybar { echo -n $1 > /dev/udp/localhost/${2:-1738}; }

To be clear, the genuine bug is that the nc invocation fails silently when the process sending output to nc is "slow". I was sending data to anybar via the (command && echo "red" || echo "green" ) | nc ... and was scratching my head as to why it wasn't working! IMHO, nc may be convenient if flawed when other options are not available, but when bash's network redirection is available it is far preferable.

@tonsky tonsky closed this as completed in 0f1e0c1 Apr 28, 2022
@tonsky
Copy link
Owner

tonsky commented Apr 28, 2022

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants