r/programming • u/vincentk • May 30 '14
First-Class Shell Scripting Support For Python
https://amoffat.github.io/sh/5
u/jmtd May 30 '14
This can be very useful but the approach for modelling pipes (nested functions) smells a bit funny.
2
2
u/0x5f3759df May 30 '14
You could combine it with Hy;
(import-from sh grep cat) (-> (cat "/usr/share/dict/words") (grep "-E" "tag$"))2
u/tyleroderkirk Jun 02 '14
For the lazy: "Hy is a wonderful dialect of Lisp that’s embedded in Python" - Hy documentation
1
u/Muvlon Jun 02 '14
True. I would honestly just do:
from sh import bash
bash("program1 | program2 | program3")
5
u/zbonk May 30 '14
I think it's a very cool project but I don't think I'll be ever using it outside some personal scripts because it breaks cross-platform support and dependencies can't be just installed with pip alone.
7
u/vincentk May 30 '14
It's difficult to support
bashAND remain cross-platform at the same time, if I'm not mistaken.1
6
u/gaussflayer May 30 '14
To be fair it is aimed at replacing shell scripts - which are platform specific - with something less error prone.
2
u/manojlds May 30 '14
Inspired by this, I had written cmd, for C# - https://github.com/manojlds/cmd . For me the inspiration was to see how something like this can be implemented in C#.
3
May 31 '14
Been using this for about a year and a half in production; works well. I'm slowly replacing my use of subprocess with it as I touch areas of code.
1
u/jzwinck Jun 01 '14
What benefits does this give you?
1
u/rmoorman Jun 02 '14
well ... first things which come into my mind:
check for program availability; I prefer to "instantiate" the programs I want to use within the first executed lines of my scripts and in case it is not available ... the script fails early
it provides a nice abstraction around program execution and lets you chain commands together in a more pythonic way
you can provide arguments to programs just as you would in case of ordinary functions
you can "prepare" commands and pass them around inside your program
you can take advantage of generators easily
... and likely a few more
2
u/jivatmann May 30 '14
Some time ago i came across this nice trick, which can be used for switching between shell and python in one single script file: #!/bin/sh
"""":
echo "hello this is shell, calling echo function, my PID is $$"
exec python "$0" "$@"
"""
import sys
import time
import os
time.sleep(2)
print "this is python PID " + os.getpid() + " is mine, now"
time.sleep(2)
sys.exit(0)2
May 30 '14
Tcl:
puts [exec ifconfig wlan0]
puts [exec ls -l]
set longest_line [exec wc $file -L]It's funny that I got to discover Tcl only 1 year ago, because I wanted a better shell "glue" language than bash. I think it would make a fine replacement for the work that shell languages are used nowadays. As opposed to other in current use languages, "escaping" out to shell commands feels way less intrusive.
1
0
u/co_dh May 30 '14
I believe this module is a over design. it can be implement in one function as below.
from subprocess import check_output, STDOUT def sh(cmd): print check_output(cmd, shell = True, stderr=STDOUT)
1
u/cybercobra May 30 '14
shell=Trueis dangerous security-wise and error-prone as soon as even slightly exotic filenames are involved.2
u/bready May 31 '14
I hear this all the time, but I don't understand. Any script you would run in bash is just as open to exploitation/errors when receiving runtime input, so what is the specific problem here?
1
u/cybercobra May 31 '14
Just because bash kinda sucks doesn't mean a Python replacement has to suck too. If you use shell=False, then unlike in bash, filenames with spaces or shell metacharacters in them will work just fine, no need to remember to escape/quote them every time. No need to worry about a malicious "filename" of
;rm -rf /#either.
0
15
u/[deleted] May 30 '14
Reminds a lot of plumbum module.