In Episode 41, we discussed each of these examples as part of our introduction to scripting. Listen to the episode, follow the tutorials, or just download the scripts and try them out. We openly admit that we are scripting noobs, and that more advanced users might look at our examples and point out that there were better ways. Ask a professional baker or brewer how to make bread or wine, and you might walk away with tons of technical information, and no idea where to start. It may be to your advantage then, that we have only recently started down the trail that you are about to begin, and clearly remember where the stumbles are. Or we might be full of it. In either case we will try not to mislead you or give you bogus information.
A shell script is a text file. You can create it with your text editor. The script calls an interpreter to run it. We are going to be using the common "Bash" interpreter, so when we say "shell script" here, we really mean a bash shell script.
Rehearse! If a line of script won't work by typing it into the terminal, it won't work in your script. Save yourself a lot of headaches and give your commands a dry run. You can copy and paste your command lines into the terminal to "proof read" them.
The first line of any shell script is a "shebang" and then a file path to follow to find the interpreter. The shebang is simply 2 characters, a # and a !. The shebang identifies the file as a script. Since we are writing bash scripts, the path to the interpreter is /bin/bash. Thus, the first line of our script needs to be #!/bin/bash with no spaces. (Hereafter shebang bin bash.) For scripts that are more "generic" and portable from one *NIX system to another, you can use #!/bin/sh instead. This points to the default shell program for your Linux installation, whether it is bash, dash, ash or something else.
Before you run the script, you must change the file permissions. Type chmod 755 filename (or chmod +x filename) at the command line or right-click on the file and change it via the file's properties. To run the script from the command line type ./filename with no spaces.
In theory, most of what we present here should work with most Unix style systems, including Mac OSX. Your results may vary.
Type or copy these scripts into a text editor, save them, change their permissions, then run them. You could also just download the completed script if you wish.
Ready to get your geek on? Let's make a GUI to remind you of important dates and times. That's right. A home brew graphical user interface from a script!
The application that we are going to use for the GUI is called zenity. Long time listeners may have heard us complain that zenity has terrible documentation. Not so if you are running Ubuntu. Click on:
System > Help and Support.
When the help window opens, enter "zenity" into the search window. There you will find excellent documentation with examples.
If we are going to use zenity, we need to install it first. Enter this in a terminal:
sudo apt-get install zenity
You could do that from your package manager if you don't happen to be running a distro that supports apt-get.
Now comes the script.
So, big deal. The script works, but it happens right now. What about making it run in the future when I want it to? That's where cron comes in. Let's go and edit your cron schedule. From the terminal:
The "-e" part means that we want to edit. The terminal now displays the cron editor in your default editor. (With K/Ubuntu, this will be a program called nano. Your distribution may use a different default editor.) Let's look at that. The first line looks like this:
# m h dom mon dow command
You know what that # means. The line won't be executed. You could delete that line and everything would still work, but don't. It's telling you the proper way to enter a cron job. Here's what it's saying.
minute, hour, day of month, month, day of week, command
You will be be entering your cron jobs, one per line, below that reminder. As an example, let's say that you want to execute "notify.sh" at 9:15 AM on every Monday. You would add this line:
15 9 * * 1 ./notify.sh
The "*" character is a wild card. In this case it means; "I don't care about the day of the month or month. Just do it on Mondays at 9:15 AM.
Here's an example to make that more clear:
* * * * * ./notify.sh
That would execute every minute, of every hour, of every day of the month, of every month. Yeah. You would get tired of that in a hurry, but you see the point.
So let's try this thing out. Look at your system clock and pick a time that's about 10 minutes into the future. Let's say it's 1:30 PM. You would type this on the line:
30 13 * * * ./notify.sh
Now you will exit the editor. If you are using nano, you will see the clues at the bottom of the window. You want to hit Ctrl-X (^X).
You will be asked if you want to save. Hit "y" and enter.
You will be asked where you want the file saved. Don't worry about it. Just hit enter again.
Crontab will check the addition as it exits. If there's a huge mistake, it will complain and ask if you want to fix it. If not, your script is going to run at the specified time.
You can specify a range with a dash. If you want a command to run Monday through Friday at 3:00 PM:
0 15 * * 1-5 command
Adding a comment for each cron job will help you to stay organized.
If your computer is turned off when the cron job should run, nothing will happen and your spouse will kill you for forgetting your anniversary. Have a backup plan!
If you followed that, pat yourself on the back. You are an official Linux geek!