3 ms·
Why ever resort to shell scripts when we have languages like ruby / python for anything more complex than installs? Clarity is king people
by meow_mix 8y ago
Why ever resort to shell scripts when we have languages like ruby / python for anything more complex than installs?
Clarity is king people
- scbrg 8y agoBecause foo | awk '/bar/ {print $3}' is more clear than import subprocess foo = subprocess.Popen(['foo'], stdout=subprocess.PIPE) for line in foo.stdout: if 'bar' in line: try: print(line.split()[2]) except IndexError: print('') Sometimes shell scripts are more clear. Especially for tasks that involve running lots of external commands.
- meow_mix 8y agoNobody doubts that pipes and languages like awk are great for one liners, but I think that's a little besides the point of this post, which is advocating for things like the use of bash arrays: ``` array=( a b ) array+=(c) if [ ${#array[@]} -gt 0 ]; then rm -- "${array[@]}" fi ``` ``` array = [a, b] array << c array.map { |i| `rm #{i}` } if array.length > 0 ``` There's also nobody stopping you from using text processing tools like awk and sed, or bash one liners in ruby/python either, but I think we should leave the logic and arrays for scripting languages, no?
- scbrg 8y agoYou're moving the goalposts! Previously it was "anything but install scripts". Now it's "logic and arrays". I actually think we agree with each other, but we express it differently. I never write bash scripts, I write POSIX shell, so all the array juggling of bash is something I never deal with. As you say, by the time you need arrays, you should have switched languages already. That said, I think there's a fairly large domain of problems - apart from install scripts - that are better solved with shell scripts, because of their clarity. Anything which relies on invokation of lots of other tools, and in particular problems that fit the pipe model (take output from this tool, extract interesting bits from it, and feed it to that tool, etc). And this I say as an otherwise almost slightly fanatical Pythonista :-)