This is the 3rd time that I have quoted Jason Cannon's email about "The biggest mistake shell scripters make" to whom I am giving every speck of credit to for his words (of course!) This email arrived in my Inbox on Tuesday, August 16, 2022, at 5:50 AM. Here's his email:
Hello,
From time to time one of my students will ask me to take a look at one of their shell scripts that they are having trouble with.
If you know anything about me at all, you know that I love three things. Linux, troubleshooting, and shell scripting. (Make that four things... teaching my love of Linux, troubleshooting and shell scripting!)
The offer to review a shell script is almost irresistible to me, so I'll say "Sure, I'll look at it."
Reviewing scripts gives me the chance to see many common mistakes. And the number one biggest mistake of all is:
Not defining exactly what the shell script is supposed to do!
What is supposed to happen when I run this shell script?
Why does this script exist?
Can I at least get a simple comment at the top of the script justifying its existence?!? (Um, no.)
By the way, I'm not judging or blaming. Again, this is a very common mistake... a mistake I used to make when I first started scripting, too. This is coming from a place of caring. I *want* people to write awesome shell scripts and I *love* helping them do so!
So, here's my first piece of advice to any (new) shell scripter:
START with the END in mind!
Why?
Well... sometimes people make assumptions about HOW to accomplish their larger goal. You may think you need to do A, B, C, D, E, F, G, and so on to get to Z, but in reality there may be a shortcut that takes you straight from A TO Z. (Shortcuts like bash builtins, predefined shell variables, Linux commands that you didn't even know existed, etc.)
It's the age-old case of not being able to see the forest for the trees and getting lost in the weeds. :-)
You could easily end up with this:
When all you needed was this:
(Shameless plug: If want to learn all the best scripting "shortcuts," check out my latest project-based shell scripting class here.)
Another trap you can fall into is biting off more than you can chew. Once you have a goal in mind for your script, keep it focused on the essentials.· What MUST this script do?
· If this script could only do one thing, what would that be?
· What will get me 90% of the way to my goal with the least amount of effort?
Start there. Implement that first, then circle back around to the "nice-to-haves" or bigger goals for your script.
It's way easier to light a room in your house than it is to illuminate the entire neighborhood.
Now you know where to start. In the next email, you'll learn how to write a shell script.
All the best,
Jason
P.S. Want to vastly improve your shell scripting skills quickly? Want help from me personally? Then click here.
Unsubscribe | 400 Locust Street, Suite 400. Des Moines, IA 50309
No comments:
Post a Comment