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….
Sometimes, we all have to look back down the path we’ve taken to more fully understand that path that lies ahead.
I started my Linux Adventure a bit late in life. I’ve always had an interest in all things technical. My career for the majority of my working life was as an electronics technician (component level repair). I had aspirations at one time of gaining an engineering degree in electronics; other paths were taken, though.
My first experience with computers and programming and such was in 1979-80, when I was attending tech school. We were trained on kit-made 8080A computers that were so primitive that they were programmed directly via octal machine code. The outputs of the machines were led light displays. This was ridiculously simplistic by today’s computing standards.
I didn’t choose computers as my field of endeavor, though. I was much more interested in RF (radio frequency) and audio electronics. That decision probably made for a much different life than others would have. I regret lacking the vision that others had regarding the future of the computer. Hindsight shows me that I would have enjoyed a career in that field very much.
I did not have much interaction with computers from the early 80s till about 2000, with the exception of some fun times with a Commodore SX64 and some intriguing games from a company called Infocom. Oh, I had some experience with PC-type systems at the workplace in the early 90s, but I never really developed a passion for them.
I remember in the late 90s sitting at my kitchen table reading computer store ads and dreaming about getting a system for my home. I never could justify the money for it, unfortunately. I had other priorities. In 2000, my brother bought a new system for himself and passed his old system on to me. It was a little Pentium I – 90Mhz machine. I set that guy up and signed on to a bunch of free dial-up ISPs and off I went…
My current passion with computers and operating systems came about directly from my initial experiences with the Internet in 2000. Yeah, I was a little late coming to the party. I’ve been trying to catch up ever since. I have come a long way, though. I’ve been building my own systems since 2001 or so. I crossed over from MS Windows to GNU/Linux in 2006. I’m currently reading and learning all I can about the GNU/Linux operating system.
I made some resolutions in the new year to learn more about specific Linux subjects; one in particular was shell scripting. I’m currently reading and experimenting with that now. I’m also publishing some basic lessons regarding this stuff as I go along. I learned a long time ago that a great method for learning is to learn by teaching. I have to research and learn something before I can write an article about it here.
Whatever I learn, I like to pass on to others. That is the beating heart of the GNU/Linux Open Source community. I have learned so much from the selfless acts of others in this community that I am driven to give something back. It is a mission of mine to educate, to assist, to entertain, and to ease the transition of new Linux Adventurers into this wonderful community. I am no guru when it comes to Linux, but I have gained enough knowledge to get around without getting lost too often. I have much to learn yet, but when I do learn it, I’ll be here or somewhere helping others to learn it too.
A man called Bruno inspired me.
I would one day love to earn a living writing technical related articles or books regarding GNU/Linux. I would like to be employed in some fashion that would allow me to use my knowledge in a GNU/Linux business environment; as a systems administrator or a technical writer for some company or other. Sadly, my late-coming to the party and the fact that I’m no young spring chicken anymore has hindered my abilities to secure any positions like these. I’m totally self-taught and hold no industry certifications. I would love to attend school again to learn more in this field, but again… it doesn’t always work out that way.
I’m not at all sure what my future path is going to be like. It’s a day-to-day thing right now. However, I will always be learning; and with any luck, I’ll always be here passing it along to you.
Thanks for reading/commenting.