Live data from Hacker News

Show HN: Using Rust to write shell-script like tasks

github.com

71–80 of 83 posts

Re: Show HN: Using Rust to write shell-script like tasks

#71

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!

I’ve been playing with xonsh lately and really liking it so far! From what I can tell it’s pretty close to feature parity with fish shell which has a lot of nice things like command auto completion but you don’t have to learn yet another shell syntax it’s just python. Wrote up a quick trip report at https://blog.jamesolds.me/post/xonsh-aws-example/

Re: Show HN: Using Rust to write shell-script like tasks

#72
post #57

Earlier 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…

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).

Re: Show HN: Using Rust to write shell-script like tasks

#73

My 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.

When I first was getting into coding while doing test engineering, this was one of my greatest complaints. The software engineers would hand me bash scripts filled with very clever, but unexplained and esoteric one-liners.

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

#74

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!

You might like my alternative design choice: https://github.com/NightMachinary/brish

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

#75
post #72

Earlier 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).

On the one hand, on Linux with the clone system call, you actually can have a "thread" that shares memory, file descriptor table, etc, but not working directory (or chroot):

        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

#78
Interesting, 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

#79
post #78

Interesting, 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 )

https://crates.io/crates/typed-html-macros

Re: Show HN: Using Rust to write shell-script like tasks

#80
post #78

Interesting, 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 )

Yeah, there's a system for macro definition where you define the language the macro accepts. It's very powerful and can probably do most of what you're imagining in this comment.
Post reply on HN