3 ms·
What I really want for scripting usecases is a language that has modern language concepts (easy to use arrays and maps, text formatting strings, etc) but that a
by liampulles 2y ago
What I really want for scripting usecases is a language that has modern language concepts (easy to use arrays and maps, text formatting strings, etc) but that allows me to call commands as easily as I could call functions.
Maybe there are existing scripting langs that make this so? Ambers approach is not bad but I feel it could be even better.
- cglong 2y agoHave you taken a look at PowerShell? It can infer whether you're invoking a command or function. Disclaimer: I work at Microsoft, but not on PowerShell.
- mst 2y agoIt's generally quite doable to write functions for that, e.g. in perl - use IPC::System::Simple qw(capture); use Sub::Install qw(install_sub); foreach my $command (qw(foo bar baz)) { install_sub $command => sub { capture($command, @_) } } ... my $output = foo($x, $y); # will throw if calling 'foo $x $y' returns non-zero (there's a Shell.pm but it's not as helpful with errors; I should probably consider rewriting it a more modern way) Note that if you're distributing code p3rl.org/App::FatPacker will produce a single fat script that has (pure perl) dependencies included so you don't need to install anything (except for having a perl VM in $PATH). There's also multiple libraries that provide a '$' function in JS (which may be why amber picked that delimeter) and then you can do let output = await $`foo ${x} ${y}`; (the template string's variable parts get autoquoted) Note that bun.sh has a 'bun compile' subcommand that bolts together your (pure JS) code and the static bun binary to produce a single file you can also copy around. I'd suggest avoiding backticks in ... everything. In shell, $() is nicer, and both perl's and ruby's backticks require effort to use safely. No idea re python but I see no reason you couldn't do the function generation thing if you wanted to, and somebody's probably librarified it already.
- klibertp 2y agoRash (https://rash-lang.org/ https://rash-lang.org/) and Oil (https://www.oilshell.org/release/latest/doc/ysh-tour.html https://www.oilshell.org/release/latest/doc/ysh-tour.html) come to mind. Xonsh, too, as mentioned. Powershell too. Most high-level languages have packages that implement DSLs for running and connecting subprocesses. Python has both `subprocess` in stdlib and things like plumbum (https://plumbum.readthedocs.io/en/latest/index.html https://plumbum.readthedocs.io/en/latest/index.html) or fabric as 3rd party packages. Scala has a little DSL ("echo 1" #| "cat" !) for this in stdlib. How lightweight the syntax is depends on how DSL-friendly the language is, so things like Ruby or Raku might be your best bets.
- liampulles 2y agoOil/ysh looks very much like what I would like - thanks!
- mattbessey 2y agotry ruby. there are several ways to call commands but the simplest is simply the command string in backticks
- coolcoder613 2y agoXonsh maybe? The syntax is something like this: text=@(ls -lh).replace('a','b').split('b')
- inbetween 2y agoMarcel the shell is surprisingly effective for that. It is python with seamless calling of external commands and piping: ls /home/jao | map (lambda f: (f, f.size)) https://github.com/geophile/marcel https://github.com/geophile/marcel