4 ms·
Some of us actually have to USE shell scripts written by other people who like to brag about only looking at the doc a couple of times, who just copy and paste
by DonHopkins 8y ago
Some of us actually have to USE shell scripts written by other people who like to brag about only looking at the doc a couple of times, who just copy and paste things they found in outdated pages on google into hodge-podges of frankenscripts, and who then run them as root at regular intervals on unattended servers.
It may be care free, fun, and effortless for you, but try being on the receiving end of that behavior some time.
RTFM! And if the manual sucks (like the Bash manual), then learn to use another language.
Instead of learning to write shell scripts better, people should learn NOT to write shell scripts, and learn a decent scripting language instead.
You will find that most decent scripting languages like Python are actually a hell of a lot easier to learn and program and maintain than shell scripts.
- mirkules 8y agoJust 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 8y 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 8y 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 8y 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 8y 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}" }
- im3w1l 8y ago> people should learn NOT to write shell scripts, Knowing the basics of shell scripting is not optional. As a dev on a unix system, it's just a question of time before you have to read and tweak other people's scripts.
- deleted 8y ago[deleted]
- DonHopkins 8y agoKnowing how to change a diaper when you're a parent is not optional. But you shouldn't shit your own pants when you don't absolutely need to. You may think you're clever enough to sneak out a little one-liner that nobody will notice, but all too often you underestimate the task, and it blossoms into a big messy shart script that other people have to deal with.
- gumby 8y agoAround 1992 I wrote a CGI at Cygnus for reporting the top high priority and important bugs, who was responsible and how old they were. It was used for years, and people would load that page several times a day as they did not want to have their name at the top of the list! I wrote it in Bash.
- DonHopkins 8y agoAt Sun in 1991, the stupidest thing I ever did was try to use xbugtool to report a bug against itself. https://www.donhopkins.com/home/catalog/unix-haters/x-windows/xbugtool.html https://www.donhopkins.com/home/catalog/unix-haters/x-window... (Well that, and installing Solaris.) https://www.donhopkins.com/home/catalog/unix-haters/slowlaris/worst-job.html https://www.donhopkins.com/home/catalog/unix-haters/slowlari... #!/bin/sh # This works...I wonder if it will get me laid progname="`echo $0 | sed 's:^\./\./:\./:'`"
- dredmorbius 8y agoCirca the same era I wrote some space-management tools in DCL. Got a support call years later when I could no longer spell that. Code can be astonishingly durable.