In our last lesson here, I scratched the surface of the basics needed to start scripting in BASH. Today, we'll learn a few more things.
Here are some fundamentals that we need to cover real quick. It's nothing too complicated.
chmod - You've seen this command in action before, I'm sure. It's important when it comes to writing shell scripts because it allows the script to become executable. This is, of course, a necessity if we want the script to actually run and do something.
#! - These two symbols used in this particular order at the beginning of a script specifies which shell we want to use to run this script. We will primarily be talking about BASH here in these lessons, but let's say we wanted to write a script that used the tcsh shell instead. The first line of the script would be:
The operating system would check that first line initially to determine what shell we intended when we wrote the script. It would then call up that shell and run the script. You'll see that a lot when programming in BASH. Now you know what it does.
# - This symbol signifies a comment. You've probably seen this many times already in configuration files like GRUB's menu.lst or your distribution's repository configuration file, maybe. It's not at all uncommon to find this symbol used in many different programming languages to alert the OS (and the user) that the data on the same line following the # is an informational comment of some sort. Comments are GOOD things when programming. Make lots of comments. They will help you and maybe others who use your program or script down the road. Here's an example of a commented snippet of script:
# ZIP-100 mknod script - 07222006 - Bruno
mknod /dev/hdb4 b 3 64
You can see the initial #! telling the OS what shell to use. In the next line, you can see the # symbol followed by some informational data. This was an actual script that I used to need to run at boot to get my ZIP-100 drive to be recognized by the OS. Newer kernels later on solved the issue by using udev, but that's a whole 'nother subject. I just wanted you to see what a comment looks like in a BASH script.
; and <newline> - These two devices, when used in the shell, denote a transition from one command to another. For example, if I had three commands called c1, c2, and c3, I could run them on the same line using the ; (semi-colon) to separate them. Or I could just put each of them on a new line. Here's what I mean...
$ c1;c2;c3 <ENTER to execute>
$ c1 <ENTER>
$ c2 <ENTER>
$ c3 <ENTER>
Pretty simple so far, huh? OK. Let's continue...
- The backward slash is used to continue a command from one line to the next. Like this:
$ echo "She sells seashells
> by the seashore." <ENTER>
She sells seashells by the seashore.
Without the , the line would have been broken when displayed by the echo command's output. Like this:
$ echo "She sells seashells
> by the seashore." <ENTER>
Sea sells seashells
by the seashore.
See the difference? Cool! OK, then... just a few more for today. We don't want to get a headache by stuffing too much in there at one time.
| and & - These two are very cool, and you'll see them a lot. The first one is called a "pipe", and that's just what it does. It pipes output of one command into the input of another. The ampersand (&) symbol tells the shell to run a command in the background. I should briefly diverge here for a moment and mention foreground and background operations so you'll understand what they are.
A quick sidebar lesson...
You can run a command in the foreground (actively running before your eyes) or in the background (hidden from view but still going). The way to do this is to use the bg and fg command modifiers in the command line. Let's look a a couple simple examples. Let's say Mary wants to run some big admin job in the background while she does other stuff in the foreground. It's pretty easy to do. First she starts big_job01 from the command line.
With big_job01 running actively in the foreground, Mary doesn't have a cursor blinking at the command line. She cant do any other work because she has to watch big_job01 outputting. To change this, Mary will "background" big_job01 to bring back her command line cursor so she can do other things.
What this combination of keystrokes will do for Mary is it will stop the process big_job01, giving this notification:
1]+ Stopped big_job01
Now Mary will get her cursor back and she can send the big_job01 to the background and get it running again.
mary@workplace:~$ bg 1
She can check to see that it's in the background and running by listing the jobs.
mary@workplace:~$ jobs -l
1]+ 4107 Running big_job01 &
Note the following & in the above line. That's telling Mary that big_job01 is running in the background. Cool, huh? Now Mary can go on about her other chores in the foreground as she normally would. When big_job01 finishes, the command line will give Mary a notification like this:
1]+ Done big_job01
OK, there you have it... a bit about backgrounding and foregrounding operations within the shell. Now let's go back to what we were talking about before.
( and ) - The parenthesis are used in shell scripting to group commands. The shell will actually create a copy of itself, called a sub-shell, for each group. Groups are seen by the shell as jobs and process IDs are created for each group. Sub-shells have their own environment; meaning they can have their own variables with values different from other sub-shells or the main shell. A bit confusing, huh? Well, let's see a quick example and then wrap up this lesson so we can all go watch C.S.I. on TV.
$ (c1 ; c2) & c3 &
In the above example, grouped command c1 and c2 would execute sequentially in the background while executing command c3 in the background. Remember that we can background commands by using the ampersand (&) symbol and separate commands by using the semi-colon (;). The prompt would return immediately after hitting ENTER on the line above because both jobs would be running in background. The shell would run the grouped commands c1 and c2 as one job and c3 as another job. Clear as chocolate pudding, huh? Don't worry. It'll all come together... I hope.
Until next time....
*This article originally appeared on my Nocturnal Slacker v1.0 site at WordPress.com.