3 ms·
> var = !cmd syntax Is this a standard anywhere outside IPython? I would love to use this sort of thing (python with seamless integration with a system shell)
by fredley 6y ago
> var = !cmd syntax
Is this a standard anywhere outside IPython? I would love to use this sort of thing (python with seamless integration with a system shell) in place of bash scripts. Yes, you can use subprocess but it's a lot less readable.
- tyingq 6y agoYou might like Perl. The syntax to invoke and interact with processes is much more terse than Python's. And there's quite a lot of history of using it instead of shell scripts. Edit: Things like: $output=`ls`; $output=qx#ls -l#; system('/bin/echo','avoid','subshell','invocation'); open("APIPELINE","ls -l |"); while (<APIPELINE>) { chomp; print "Got line: [$_]\n"; } close(APIPELINE); And access to child exit status in a way that's familiar ($?), etc.
- dale_glass 6y agoThat Perl is quite out of date. These days, Perl has 3 argument open, and can use a variable for a filehandle. So: open(my $pipeline, "-|", "ls -l"); This is a good thing, because you can treat a filehandle as a standard variable, and can separate the special stuff from the command itself. This way if you're taking in user input, they don't get to mess with that. You can also do: open(my $pipeline, "-|", "ls", "-l", $directory); Which lets you get away from the mess of correct quoting and just pass every parameter exactly as intended.
- mumblemumble 6y agoThe bang syntax is specific to ipython. I'm not sure it's really desirable outside of that context. It lacks a lot of process control features you'd want in order to make things robust for general purpose scripting purposes.
- chrisshroba 6y agoIt's also similar to a vim construct where prefixing a command with ! makes it run it as an external program instead of a vim command.
- fooblat 6y agoWhile I haven't personally used it, another commenter mentioned Xonsh[0] and that is what it appears to be: A very smooth integration of python into a shell that still feels like a unix shell. 0. https://xon.sh/ https://xon.sh/
- notagoodidea 6y agoIt depends, for other python REPL/Env not really but it is the standard way to call shell command from ed [1] for example. Julia have also a shell mode in its REPL [2]. Personally, I like to use Raku [3] instead of Python or Shell script. It make it easy to drop one-liner, call shell commands [4] and you can super easily make a script a small CLI utilities by writing a MAIN sub [5]. [1] https://www.gnu.org/software/ed/manual/ed_manual.html#Commands https://www.gnu.org/software/ed/manual/ed_manual.html#Comman... [2] https://docs.julialang.org/en/v1/stdlib/REPL/#man-shell-mode https://docs.julialang.org/en/v1/stdlib/REPL/#man-shell-mode [3] https://raku.org/ https://raku.org/ [4] https://docs.raku.org/language/quoting#index-entry-quote_qqx-Shell_quoting_with_interpolation:_qqx https://docs.raku.org/language/quoting#index-entry-quote_qqx... [5] https://docs.raku.org/language/create-cli https://docs.raku.org/language/create-cli
- neolog 6y agoThat syntax exists in `git alias` too.
- gandvor 6y agoYou might like invoke: https://www.pyinvoke.org/ https://www.pyinvoke.org/