5 ms·
Just to add, I generally prefer shell scripts over Python because it is supported in all my use-cases out-of-the-box. However, I recently discovered the power
by mirkules 7y ago
Just to add, I generally prefer shell scripts over Python because it is supported in all my use-cases out-of-the-box.
However, I recently discovered the power of Python’s Fabric scripts, and holy cow... the ability to run functions on multiple computers at the same time based on roles is amazing (even if the “join” syntax is cumbersome as hell).
I think in general, simple system things should be written as scripts. The moment you realize “this is going to take a LOT of scripting” it’s time to look at Python because chances are there’s a module for that.
- nickjj 7y agoIf you like writing things that you can execute on multiple machines take a look at Ansible. It's a Python based configuration management tool where a majority of what you write is YAML and Jinja 2 templates. But on that note, a majority of my current shell scripts are being used on my dev box to improve my day to day. Things like this: https://github.com/nickjj/invoice https://github.com/nickjj/invoice
- DonHopkins 7y agoHave you ever looked through the Linux source code to see how many lines of code and instructions and data structures and libraries and memory allocations and mappings and system calls and context switches are required to search a path for an executable and fork off and run a Unix process? It really breaks my heart to see stuff like this pointlessly wasting electricity by running on a computer that has its own "mul" instruction -- like forking off a "bc" and sending it a stream of decimal digits over a pipe just to multiply a number by 100: echo " > \$$(echo "100 * ${default_hourly}" | bc -l) 100.00 \$${default_hourly} 50 acme" Don't tell me you prefer writing shell scripts that do simple arithmetic because it's more convenient than using Python. Calling out to awk to do trivial string manipulation may impress you as "amazing" after you've hit the wall trying to convert a string to decimal hours with bash and bc, but do you really enjoy all that noisy cryptic bash syntax with all those carefully orchestrated backslashes, double dollar signs, triple angled brackets, echos and pipes and quotes, nested parens and braces, and mysteriously abbreviated dash-letter parameters, to typing "100 * default_hourly"? At least Python's "*" operator doesn't require a "-l" parameter to figure out how to multiply a number by 100. The process id is not your Linux high score. There's no need to run it up by forking so many processes all the time.
- nickjj 7y ago> echo " > \$$(echo "100 ${default_hourly}" | bc -l) 100.00 \$${default_hourly} 50 acme"* > Don't tell me you prefer writing shell scripts that do simple arithmetic because it's more convenient than using Python. That's not a fair comparison. The code you pasted isn't just doing the calculation. It's dynamically generating a human readable help menu that shows both the input and output of what the script will do. That requires quite a bit more characters and escaping. The "real" calculation to get the hourly is: amount="$(echo "${total_hours} * ${hourly_rate}" | bc -l)" Seems pretty straight forward to me. Plus, that's a really cherry picked example in Python's favor. You should evaluate the whole script's purpose. It's mostly just reading and filtering text output from a file and then performing basic math on the filtered text. Seems like a textbook case to use Bash IMO.
- DonHopkins 7y agoIt's perfectly fair. You're the one who got to cherry pick the script to use as an example, so don't complain that I cherry picked the most ridiculous line of that code. Python can generate a human readable help menu with much less trouble. Tell me one thing about that script that's easier in bash than in Python. Forking off sed? Python doesn't need to, it has string manipulation. Forking off bc? Python doesn't need to, it has arithmetic. And why are you using sed regular expressions just to do arithmetic? Now you have two problems! https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ https://blog.codinghorror.com/regular-expressions-now-you-ha... Again, look at the Linux kernel and userland source code and give me a ballpark estimate of how many instructions it requires to multiply a number by 100 by forking off bc or sed, instead of executing a single "mul" instruction. And Python code doesn't require a page of comments to explain why it has to go to so much effort and work around so many pitfalls just to convert a couple of strings to decimal hours. Why, Python even has named function parameters -- now THAT is TRULY "amazing". And in the number of keystrokes it took you to write the following comments (and the number of trials and errors it took you to finally get it right), you could have imported a hell of a lot of powerful Python modules, and read a lot of the interactive built-in help documentation about their APIs. Your time would be much better spent getting used to using Python rather than sed. to_decimal_hours () { local hours="${1}" local minutes="${2}" # [h: ] uses the "h", ":" or " " character as a split delimiter. # # If the first split argument ends with "m", then we're not dealing with # hours at all, only minutes, so now $1 becomes minutes and we divide by # 60 because you can't have more than 60 minutes in 1 hour. # # Otherwise, we have both hours ($1) and minutes ($2). The hours can be # taken as is, and like before the minutes get divided by 60. # # It's also important to send both the hours and minutes in without a # space as input, otherwise values like "30m" will end up being "30m " # which makes $1 no longer end with an "m" and it will incorrectly report # back 30.00 instead of 0.5. # # awk will automatically remove the "h" and "m" characters when it comes # time to do the addition. # # Yep, awk is amazing. I'm just getting used to using it! awk -F '[h: ]' '{ if ($1 ~ /m$/) printf("%.2f\n", ($1 / 60)); else printf("%.2f\n", $1 + ($2 / 60)) }' <<< "${hours}${minutes}" }