Introduction to Bash Scripting
Novell Cool Solutions: Feature
By Scott M. Morris
Digg This -
Posted: 20 Sep 2005
- Novell Linux Desktop
- SUSE Linux Enterprise Server
- SUSE Linux Professional
For many people who have high workloads, it's nice to automate as much of it as possible. This is especially true with tasks that are monotonously repetitive. Wouldn't it be nice to have a way to create a customized tool so that you could automate as much as possible? Fortunately, this kind of thing is built into Linux. It comes in the form of shell scripting.
It is amazing how much these scripts are used in the Linux operating system. They can be used in cronjobs, the bootup process, the shutdown process, and in the starting and stopping of various daemons and services. They don't always have to be used in such grandiose, important ways. They are unbelievably versatile, and can be used for countless tasks.
Let's take a look at how to create a very simple shell script. Open up your favorite text editor, Kate. On the very first line, we are going to tell the operating system that this is a shell script. This is done by entering "#! /bin/sh" into this line. As a small sidenote, you are also able to put "#! /bin/bash", but the other way is more compatible with other platforms. Your file should look like this so far:
Now, we need to make it do something. I'd suggest we make it say, "Hello World!", but we'll make it do something a little more useful. Let's make our script output some information about the system that we're on.
First of all, we'll output the system's memory status using the free command. Also, to make the script user-friendly, let's also have it tell the user what's going on as it does each thing. To do this, let's use the echo command. Type the following into your script:
Next, let's output the disk usage statistics of our hard drives, using the df command. Again, we'll tell the user what's going on using echo. Type the following into your script:
Finally, let's display the version of Linux that we are running, notifying the user of what is being displayed:
Now, we need to save the script. Most scripts end with a .sh extension. So, we'll save it as example.sh in our home directory. Next, we need to make it executable. So, open up a terminal window, change to the directory containing the script, and make the file executable:
[scott@work:~]$ cd ~ [scott@work:~]$ chmod +x example.sh [scott@work:~]$
Now, you are able to execute the script. Run the script from the commandline. Note that the filename is preceded with a period and a slash:
[scott@work:~]$ ./example.sh System memory status: total used free shared buffers cached Mem: 516004 499576 16428 0 80508 181104 -/+ buffers/cache: 237964 278040 Swap: 530136 0 530136 Disk usage statistics: Filesystem Size Used Avail Use% Mounted on /dev/sda6 21G 6.3G 14G 32% / tmpfs 252M 0 252M 0% /dev/shm /dev/sda2 69M 4.1M 61M 7% /boot /dev/sda7 34G 19G 16G 54% /home Linux version information: Linux version 188.8.131.52-21.9-default (geeko@buildhost) (gcc version 3.3.5 20050117 (prerelease) (SUSE Linux)) #1 Fri Aug 19 11:58:59 UTC 2005 [scott@work:~]$
Note that I had to wrap the "Linux version" line down to the next one so that it would all fit. In any case, there's the output we are expecting. You can now use this bash script to find out what platform a machine is using, along with it's disk and memory usage statistics.
Things like this can be quite useful if you are trying to troubleshoot a computer that's having problems, especially if it's a remote machine. The various uses of bash scripts are virtually endless. They certainly come in handy with repetitive tasks. They can also be used to tie commands together in a way that will be helpful to you. If you're interested in learning more about shell scripting, please visit the Advanced Bash-Scripting Guide.
Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com