Bash scripting intro (en)
From Conky PitStop
Bash Scripting Intro
By now in Linux you know what a command is, and how you put the two together to make good things happen in you Linux system. Hopefully you all did your homework and read through some of the links at the bottom of the page and now you have a better handle on how a command line interface works.
This time around, I’m going to show you what to do if you want to combine a variety of commands, have them execute in a particular order (line-by-line), and give you a specialized output. These types of arrangements are called scripts, and it’s real easy to put a simple one together. Of course, more complex scripts can be arranged to do just about anything you can imagine. This can be anything from starting certain programs automatically when your computer boots up, to cleaning out your system of unneeded files, or monitor your webserver on various ports and have the result sent to your cell phone via SMS messages.
Let’s use a very simple startup script as our example. Here is the file I use to start my Conkies with a 20-second delay, so my Gnome desktop has time to load:
#!/bin/bash #start Conky with a 20 second delay sleep 20 conky -c ~/Conky/.conkyrc #conky -c ~/Conky/conky-countdown conky -c ~/Conky/conky-email sleep 1 avant-window-navigator exit
Let’s go line by line and see what is happening here.
This is telling your computer that this script needs to be executed by the program located at /bin/sh – that’s your shell, also known as a terminal or a command line. That line identifies this as a shell script. There are other types of identifiers you might see there that would indicate whether something is a bash script, a perl script, a python script.. It could really be just about anything.
#start Conky with a 20 second delay
This is a comment line. You can tell because it starts with the # sign. In shell scrips, any line that starts with a # sign is ignored. This is a handy way to convey information about the script, or even about a particular command being issued in the script.
Since I told you up front that this was a small script that would start my Conkies on a 20 second delay, you could probably guess what line 3 does. In case you can’t, what it’s doing is telling my computer to sleep – or, do nothing – for 20 seconds before it executes the commands on the following lines.
conky -c ~/Conky/.conkyrc
Now here’s some action! That line is telling my computer that the first thing it should do when it “wakes up” from the sleep command is to run conky. As is the case with many commands that are run in the shell, the commands don’t always do what you want them to do right off the bat, and you have to use switches to make them work right for your particular application. In this case, the -c switch is telling conky that it needs to use a particular configuration file rather than the default one it would load located in /etc/conky/conky.conf if I were to simply issue the command conky. This config file is located at ~/Conky/.conkyrc
#conky -c ~/Conky/conky-countdown
You should already know what it does, since it’s a combination of two of the previous lines. We see that conky is being executed with a -c switch to load the config file located at ~/Conky/conky-countdown – but I’ve commented it out? Why would I do that? The answer is simple: Because I don’t want my conky-countdown to run at this particular time. Conky is, with the help of a Perl script, counting down the days until my wedding anniversary. When I made that conky, my anniversary was still over three months away, and it seemed kind of silly to have a countdown running for three months. But, since it’s right around a month away now, I might just reactivate it.
conky -c ~/Conky/conky-email
Again, very similar to the last couple of lines, this is another instance of conky being run with the configuration file conky-email.
What? AWN? In here?
Yes. I’ve noticed for a while that when you add AWN directly to your startup items, it executes too soon and sometimes doesn’t render properly until the desktop has finished loading. Since I already had some programs executing on a delay, I figured I’d just add AWN to the end of the list.
Finally, Line 9:
And I’m going to assume full well you know what it does, because it’s what I’m about to do.
Here’s some further reading on Bash scripting you can do. As with anything, Google is an excellent supplement to anything I provide here.
- Linux shell scripting tutorial
- LinuxCommand.org's page on shell scripts
- Advanced Bash Scripting Guide