This reminds me of the python version of this called xonsh https://xon.sh/ I really like the idea, but it was missing some simple features that bash had. I can't recall them right now but after an hour of trying to convert a simple bash script, I gave up. That was a year ago. Maybe things changed. I'll give this and xonsh a go again because I just really dislike bash. Thanks for the project!
Show HN: Using Rust to write shell-script like tasks
71–80 of 83 posts
Re: Show HN: Using Rust to write shell-script like tasks
#72Earlier quoted context omitted.
‘cd’ is a shell builtin so you couldn’t use ‘cd’ in any of these solutions unless they then spawn a shell instance...and that worries me if you are because then you really might as well just have a separate .sh file and launch that instead (at least that is more auditable with tools like Shellcheck than any inlined code would be).
As I read the parent comment, the broad context is turning "shell-like behavior" into rust code, and the comment is choosing to talk about that projection by focusing on elements in the source and assuming that it's understood that they're really talking about the resulting image. You can't use the shell's cd, but you can call chdir and set the working directory - and hopefully you can do that for only part of your p…
Re: Show HN: Using Rust to write shell-script like tasks
#73My god, this super useful when you have a mix o shell commands and processing text output from them. Bash isn’t particularly easy to work with parsing non-trivial strings in a readable way (I’m looking at you awk)
I apologize, but I must disagree. Awk is literally amazing once you get used to writing actual scripts instead of trying for the ever-elusive and often-untenable one-liners. Noone is using `python -c` syntax for constructing one-liners and i think thats helping adoption of python. I have no idea why one-liners are seen as desirable when they're often hard to read and debug.
Whenever something didn't work (and it didn't, because perfectly interfacing with embedded hardware is tough), I had two options: spend literal hours on Google and Stack Overflow, or go stand outside their cubicle and hope they had time for me.
I'll take a verbose function with clearly-followable logic over an amazing one-liner with a maze of options and hacks any day.
Re: Show HN: Using Rust to write shell-script like tasks
#74This reminds me of the python version of this called xonsh https://xon.sh/ I really like the idea, but it was missing some simple features that bash had. I can't recall them right now but after an hour of trying to convert a simple bash script, I gave up. That was a year ago. Maybe things changed. I'll give this and xonsh a go again because I just really dislike bash. Thanks for the project!
Xonsh is a superset of Python, which introduces a lot of complexity for little gain. Brish chooses to use Python metaprogramming abilities to solve the problem within the language itself, and so is a much simpler solution.
Re: Show HN: Using Rust to write shell-script like tasks
#75Earlier quoted context omitted.
As I read the parent comment, the broad context is turning "shell-like behavior" into rust code, and the comment is choosing to talk about that projection by focusing on elements in the source and assuming that it's understood that they're really talking about the resulting image. You can't use the shell's cd, but you can call chdir and set the working directory - and hopefully you can do that for only part of your p…
The problem is you can’t have two different threads operating in different working directories. One “cd” would overwrite another. You could have different processes but then you’re now recreating a shell, in which case you might as well just write it in Bash (for example).
If CLONE_FS is not set, the child process works on a copy of
the filesystem information of the calling process at the time of the
clone() call. Calls to chroot(2), chdir(2), or umask(2) performed later
by one of the processes do not affect the other process.
On the other hand: what you say is true of posix threads, code built atop clone is unlikely to be portable, etc, etc.More generally, it's very much the case that the process-global nature of the working directory makes some things tricky. On the other hand, there are ways around that.
If the only pieces that need to reference the working directory are processes you're spawning, the answer is simple - carry a description of the intended working directory through your computation, and actually chdir between the fork and the exec.
If the only pieces that need to reference the working directory are things you will be writing as a part of the current project, you can use "at variants" (openat, fstatat, unlinkat, etc).
You can stitch these two together as needed.
What's awkward is if you need to use library code that references the current working directory and does not use "at variants". In that case, you could play some awkward game with a mutex, setting the cwd only for the duration of individual operations and restoring it afterward (although that does require knowing when these operations may be performed).
Or you could fork your process along the lines you need to draw. Splitting your view of memory raises questions of IPC. In particular, if you want to pass language-specified data structures that gets complicated - particularly if they may contain references. If you can get away with treating everything as passing streams of bytes over actual pipes, it's pretty straightforward. To my mind, this is probably most of what's meant by "shell-script like", and while you're recreating part of a shell, it's actually not very much of a shell, it should be well contained in the library, and you have room for a much better story around things like error handling. I don't have a sense of whether any of the particular libraries discussed here are actually addressing exactly this issue or whether they actually do a good job of any of it.
Re: Show HN: Using Rust to write shell-script like tasks
#76Nothing can fix the fact, that pipes carry dumb byte streams. Powershell addressed this, but sadly remains unpopular in Unix crowd
Re: Show HN: Using Rust to write shell-script like tasks
#77Re: Show HN: Using Rust to write shell-script like tasks
#78 // valid rust code and shell code, no strings
run_cmd!(du -ah . | sort -hr | head -n 10)?;
How does rust parse the statement within run_cmd()? Can rust parse other languages like this? run_html!(COOL)Re: Show HN: Using Rust to write shell-script like tasks
#79Interesting, it seems that it allows the exact syntax of shell commands, without using strings. // valid rust code and shell code, no strings run_cmd!(du -ah . | sort -hr | head -n 10)?; How does rust parse the statement within run_cmd() ? Can rust parse other languages like this? run_html!( COOL )
Re: Show HN: Using Rust to write shell-script like tasks
#80Interesting, it seems that it allows the exact syntax of shell commands, without using strings. // valid rust code and shell code, no strings run_cmd!(du -ah . | sort -hr | head -n 10)?; How does rust parse the statement within run_cmd() ? Can rust parse other languages like this? run_html!( COOL )