http://goinglinux.com/images/GoingLinux85ShadowOutline.png

 Going Linux

   for computer users who just want to use Linux to get things done


Shell Scripting Tutorials

Updated: 29-Aug-2008

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.


The date.sh script

Download: date.sh

#!/bin/bash
echo "The current date and time."
date

The calendar.sh script

Download: calendar.sh

#!/bin/bash
echo "Calendar display."
echo "Enter a month. (1 to 12)"
read month
echo "Enter a year. (4 digits)"
read year
cal $month $year

The fetchglp.sh script

Download: fetchglp.sh

#!/bin/bash

# This script gets an episode of Going Linux.

echo "Enter an episode number. Example 041 for episode 41."

read episode

# The url of all episodes starts the same.
# For instance http://www.archive.org/download/glp038/glp038.mp3
# starts with http://www.archive.org/download/glp
# then the episode number. Then glp. The episode number again,
# and .mp3
# Let's put the first part into a string variable.

urlstart="http://www.archive.org/download/glp"

# We are going to use wget to fetch the file.
# Replace .mp3 with .ogg if you like.

wget "$urlstart$episode/glp$episode.mp3"


The notify.sh script

Download: notify.sh

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.

#!/bin/bash
# Example script from the Going Linux podcast. http://goinglinux.com/

# Opens a window to remind you of things. Change the text to whatever you like.
# Make as many copies as you like and give them distinct names.
# Cron will display them at the specified dates and times.
# See cron instructions in the Going Linux show notes for episode 41.
# http//shownotes.goinglinux.com#glp041.

# The following command is a work-around.
# It gives cron access to the display.
# I found it in the Ubuntu forums.

export DISPLAY=:0

# Here's the command that opens a notification window.
# It's copied from the zenity help pages in Ubuntu.

zenity --info --text="Time to listen to Going Linux!!"


The cron job tutorial

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:

crontab -e

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.

Done!

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.

Notes:

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.

# Warn me when it's Friday the 13th. One minute after midnight .
1 0 13 * 5 unlucky.sh

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!

Site Created with theMaker for Linux

Theme music for the Going Linux podcast is generously provided by Mark Blasco. http://www.podcastthemes.com
Creative Commons License Going Linux Podcast by Larry Bushey is licensed under a Creative Commons Attribution 4.0 International License.