485 points · by ducktective · 5y ago · 83 comments
Also, I wrote this plog function (for pipe-log, or something):
plog() {
local msg=${1:-plog}
tee >(sed -e "s/^/[$msg] /" | cat 1>&2 )
}
That allows me to see what's happening in the middle steps of the pipeline.I ended up compiling a list of tricks like that in https://raimonster.com/scripting-field-guide/ , in case anyone is interested (and feedback is appreciated)
Learned several new bashisms which i will put into my computer tip memorization deck. Thank you for writing.
[1]: https://github.com/alebcay/awesome-shell/pull/424/files
Do you have a standard template you use for your scripts? You kind of touch on this in section 4.20.1 but don't go too deep.
Also have you done any work with writing zsh (or bash) completions? I feel like having good completions would make my scripts magnitudes more handy, but I've had trouble finding good starter resources.
At the start it provides a stacktrace function which can also be used to notify you if your script has failed (e.g. via xmpp, e-mail, etc.). It is not my own creation, as I found it somewhere a few years ago, but found it super useful over time.
In addition, there are two versions for checking for dependencies. The first lets you communicate instructions for every tool that is required while the second can be used if you simply want to communicate the list of programs that is missing.
The third part is about named parameters, which can be helpful if you don't want to use positional parameters.
I never liked those "bare minimum templates" that are 100 lines, because usually the scripts I write start being just a couple dozen lines. If they grow, I add things on a need basis, but the ratio of logic/template has to be >0 always.
wrt completions, doing smart completions is hard, and the zsh ones are not very intuitive to me. I try to make my scripts work with "compdef _gnu_generic ". It goes a long way, and it's an incentive to write proper --help
tee >(sed -e "s/^/[$msg] /" 1>&2)I wish somebidy had done this for python3.
And here's the EmacsConf video for those interested: https://www.youtube.com/watch?v=HKJMDJ4i-XI
Side effects always seems like potential disasters for me.
For details, see: https://github.com/akavel/up/releases/tag/v0.3
I kind of wish LuaJIT had something like that.
Took me a while to get the lucky 10k reference ;)
Another system with some different but related capabilities is userland: https://hisham.hm/userland/
As to another huge and absolutely awesome emerging contender in this space, see https://luna-lang.org
Although the self-proud part of me feels a bit disappointed that someone else got there first, I'm actually glad that it happened (it also means that my idea was sound). I didn't even realise that something like that existed already. Kudos to both the developer and the OP!
The reason development stalled for so long is because personally I'm not sure this is something that should be widely supported. Even with murex, which has an internal catalogue of known safe(ish) commands, the dangers of tools like this is pretty high. Running this against a regular shell which doesn't even do that seems like potential suicide.
Combine it with syscall logging and you might actually have a great tool for figuring out exactly how a command is affecting your system.
I don't think it is a solvable problem though. There is no way to know ahead of time which usage is safe and which is destructive because of the variety of UNIX-like configurations out there, variety of coreutils and variety of optional flags. Not to mention all the 3rd party tools out there. And some seemingly destructive usages are vital for safe usage (eg tmp files). Worse still, by the time you build a safe container you've have spent more time spinning up a sandbox than you've had spent using the CLI preview tools anyways (yeah ZFS could help here but few people run ZFS on their dev machines and you still haven't solved mocking APIs for connected devices).
I just can't see how this can be achieved safely without completely redesigning how SYSCALLs work from the ground up to follow a method more akin to iOS or FireFox's on-demand permissions. At which point you've gone well beyond the realm of "yak shaving"
Improving Shell Workflows with Fzf (jambor.dev)
https://news.ycombinator.com/item?id=26634419
221 points by todsacerdoti 22 hours ago | 51 comments
- not having to append `| head -n $ROWS` or something similar on every command (yet having the view as-if-you-typed-it, but at the same time being able to scroll down further through the results; and having them visible as you continue tweaking, unlike `less` which you have to quit to tweak the pipeline),
- not having to press up-arrow-key, then alt-b / alt-f to a position in the pipeline you want to tweak after every Enter (e.g. just before the `| head...`, but sometimes a bit more back),
- sometimes also not having to dump the initial input from a command into a file, and to wait until it's finished, but being able to immediately start filtering even while the input is still being generated.
Once I just had those ideas, I felt pain every time I repeated the dance, enough that it made me push on up even when I was super tired of working on it. The pain of not having it when I know it could exist was even bigger.
I'm with you. I can't see myself preferring this over using my shell's interactive prompt, history, and a use of head/less at the end.
Thanks to the author and OP for sharing and it's good for other people if they find this useful, though.
EDIT: Maybe I could find this useful when the command that provides `up`'s input is not something that can be repeated for the same input. But even then, you can put it in the background with its output redirected to a file, then make the command that you would build inside `up` from a pipe like `tail -fn+1 bg-job.out | cmd`.
Powershell for example has an IDE(MS style) that is damn helpful by providing breakpoints/intraspection but this sits at a lower/faster level for just inspecting the initial pipings.
UNIX shells could approximate it with support for shared objects, D-Bus/XPC/IPC, but few make use of it.
I know that there is Nushell (1) that tries to re-do the concept of shell with ideas taken from Powershell.
> If ‘|&’ is used, command1’s standard error, in addition to its standard output, is connected to command2’s standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error to the standard output is performed after any redirections specified by the command.
I will definitely try this and see how it combines with ripgrep/rg.
On subpar hardware that site is a single collection of annoyances, including window-filling loading screens.
That said I tried UP and it works very nicely for what I need.
[1]: https://github.com/pixelb/scripts/blob/master/scripts/ansi2h...
[2]: https://unix.stackexchange.com/questions/44956/converting-co...
Lol!