I’ve read books and watched tutorials on the subject of Linux, that start right out with runlevels, do a good job of explaining the init feature, and manage to loose me in a state of confusion and despair.
What I did learn, very early on, was that I could use Linux and get along just fine without understanding runlevels at all, thank you very much.
At the same time, there was always a little voice at the back of my head, telling me that unless I understood what appears to be one of the most fundamental principles of the operating system that I’m trying to learn, then why I am bothering to try?
Guess what? I looked into runlevels, and on one level — pun intended — I now understand them. Now I can continue to live, with runlevels functioning quietly in the background, without giving them a second thought.
If anyone else is in the same boat as I once was, read on. You might just learn enough to be able to appreciate their quiet efficiency.
Cron is a software utility that schedules jobs to be run, or automates tasks in the background.
Each job can be scheduled to run periodically (at specific intervals ranging from once every minute to once every year).
The cron utility is a great asset on a server that runs 24/7, when used to perform scheduled maintenance, etc.
On a PC, the cron utility can also be useful, but I personally think that it’s pranking potential is unparalleled.
This bash shell scripting series has been intended as a comprehensive introduction to shell scripting, but is lacking many details that are essential to shell script mastery.
Continuing on, you could research topics such as STDIN, command substitution, substitution operators, pattern matching, etc.
I recommend Mastering Unix Shell Scripting and Beginning the Linux Command Line to learn more about shell scripting.
I also recommend Shell Check to help weed out any bugs (or unwanted features) in your shell scripts.
In the meantime, I threw together a little cheat sheet that includes most of the points that we have covered, as a quick-reference guide.
The cheat sheet is available exclusively to my mailing list subscribers.
Now, please excuse me while I go write a script to automate my backup process.
I personally believe that there are as many ways to write a shell script as there are people writing shell scripts.
Each individual, even if they follow similar thought processes, will apply themselves to the task with various levels of knowledge, efficiency, and dedication.
One thing I think that all coders have in common, is the tendency to re-use their code — especially after they learn to write it well, and are not constantly improving it.
There is one means of re-using code that is essential to learn, as it can enable a single block of code to be called up for use multiple times within a script.
Functions — specifically user-defined functions — are the means by which blocks of code can be re-used within a script.
One of the earliest programming challenges that I was presented with was very simple: add all the numbers from one to fifty.
The problem that I had with this challenge was also very simple; I wasn’t allowed to use a calculator, or paper, or even to solve it in my head.
I was supposed to write a program that did the math for me.
At that time — I was thirteen, and seemingly clueless about logic — it seemed insurmountable. I eventually gave up.
Two years later I tried again, and that time I had a Eureka! moment when I learned about loops. Suddenly I wondered why it had ever even sounded hard. Ah, hindsight!
The if statement can be game-changing in a shell script’s logic process, but it is not the only option, and therefore not the ideal solution in every instance.
It is possible, for example, to use the if-elif-else-fi process to evaluate the value of a variable, and perform different actions based on its value, but there is a more clean-cut solution.
The case statement — or switch statement — is that solution.
One thing that you will probably realize early on when planning out a shell script, is that you cannot possibly anticipate every single outcome.
The best that you can do is plan for the outcomes and possibilities that you can anticipate, and funnel the remainders into a catch-all.
This is made possible by the most common control structure: the if statement.
I like to think of programming (scripting) as a series of decisions made based on a system of reasoning — a process otherwise known as logic.
Logic is something that we all make use of every day when we decide what to do, and how and in what order to do it. In such cases, logic is usually an unconscious process based on actions and consequences.
We employ more conscious forms of logic when solving problems and deliberately working through processes to achieve a desired result.
Programming (scripting) employs a very deliberate form of logic, brought to pass through the use of control structures, which are dependent on various conditions presented to them.
In life and programming alike, results can be controlled by whether or not conditions are met, and how and to what degree they are met. But I gotta tell you, it’s a lot easier to anticipate and control conditions in a program than it is in real life!
In order to handle shell script arguments, it is needful to know that the arguments are required.
It can be a big hassle for people running the script to figure out what exactly is required to be able to run it.
In fact, in many cases — such as those where the user does not know how to write and thus read the bash script code — using a script correctly can be just about impossible.
To solve this problem, there is a little something called user input, which allows a script to be written in such a way as to prompt the user for input, wait until it is received, and carry on with execution.
When this solution is in use, any further issues usually boil down to PEBCAK*.
Arguments bring up thoughts of heated exchanges, differences of opinion, and persuasive reasoning — unless you’re a programmer.
A programmer knows that an argument is actually a value or data that is passed to a function or a program when it is called.
In fact, if you’ve even so much as used the command line, and typed in various commands, you have no doubt used arguments with those commands already.
In bash shell scripts, arguments can be added to the command line when a script is executed, and those arguments can be read/accessed within the script.
Special variables are used in shell scripts to access command line arguments, so to begin with, let’s take a look at the special variables available.