System Administrators have many monotonous tasks. In a some cases, it's just a matter of clicking the right thing at the right time. Some of this can be automated via a series of shell instructions - or Shell Scripting. So, we begin the first of many posts on shell scripting - specifically the Bash shell.
We are focusing on the Bash shell simply because it is the default shell for most of the distributions out there. That, and anyone who wants to switch to tcsh, or zsh, or any of the others, likely does not need an introduction to scripting.
So, let's begin. What IS a shell script? Well Johnny, it's nothing more than a file that contains commands. But oh Master, what kind of commands? Any command you can enter at a command prompt. But oh Master, that sounds too simple. Yes Johnny, it does, but it IS that simple, but deceptively so...
At the most basic, a shell script is nothing more than a text file that contains a series of commands that can otherwise be entered verbatim on the command line. So, let's take a routine task we've ALL had to worry about at one point and script it. Backups. Backing up data is critical, if you don't want to loose your job, or more importantly loose all your hard work. But it's SUCH a boring task, that it just screams to be automated. We'll take this idea and expand on it over time to make a more and more robust backup script. But we'll start very very simply.
Create a new text file. Enter this on the first line:
This line tells the script interpreter that this is a Bash script. It's possible to run other types of script (including Perl and PHP) from within a bash shell. Yep, that's right, you can run PHP at the command line. But that's a different topic...
You can include comments in the script file by using the '#' character (no quotes). The #! is a special character combination as seen above, and should not be used. Any text after the # character is ignored. Empty lines are also ignored.
Now, let's assume we just want to back up the home directory to another location. We might do this with a simple command like:
cp -r /home /backup
So, if this command is suitable, we plug it into our script file. The script file might now look like this:
#!/bin/bash #copy everything in the /home folder to the /backup folder cp -r /home /backup
Congratulations! You have written your first script. Save the file so we don't loose that massive amount of work. But we're not done yet. Writing the script is only step one. We now need to test it. To do so, we need to make sure the script is executable. We do this by modifying the permissions:
chmod 755 /home/me/myscriptfile OR chmod x+o /home/me/myscriptfile
(a quick Google Search might be handy if you don't understand how permissions are handled in Linux.)
Now that we have made the file executable, we need to run the script to make sure it works properly. To do so, you either specify the full path to the script file, or you change into the directory containing it, and use the dot slash notation:
/home/me/myscriptfile OR cd /home/me ./myscriptfile
At that point, the script should be running. BE VERY CAREFUL! Make sure you have the directory paths entered correctly. You could potentially overright some critical data if you get them wrong.
You might see some errors occur. If you get one indicate script not found, then you missed the step to make it executable, or you have specified the wrong path to it. If you get an error from the copy command (like the target directory doesn't exist), then you need to address this. You can do so either manually, or in the script itself (which would make the script more robust/versatile). Once you have fixed all the errors, the script should run just fine, and result in a copy of your /home directory contents.
Where to from here? Experiment with other common command line commands - like 'ls'... Join more than one command together (one per line). Or make the script run periodically by setting it up as a scheduled job. We'll talk about this in the next post.