Beginning Shell Scripting

OK, let’s continue on with our lessons about the Linux Shell and the command line.

Today, we’re going to learn how to write a script that you can run from the command line that will actually do stuff. It’s a very simple script, so don’t expect anything spectacular. I was wondering what I could use as an example script for this lesson when I remembered a question someone at LinuxQuestions.org asked the other day about how to tell which users are currently logged into a Linux system. Let’s make a little script to give us that information.

Let’s say that Mary wants to see who is logged in on the Linux system that she maintains for her company. She could easily do this with one simple command from the command line:

mary@workplace:~$ who

bill           pts/4       April 10 09:12

james       pts/5       April 10 09:30

marjory    pts/11     April 10 10:02

ken           pts/16     April 10 10:31

That was pretty easy, right? What if Mary wanted to check this periodically during the day? Well, she could write a small shell script with the proper permissions that she could execute any time she wanted from the command line or the terminal within her graphical user interface. Let’s find out how she might do that.

Mary would first open the vim editor with the file name that she plans to use for this script:

mary@workplace:~$ vim onsys

Vim would faithfully carry out the command and open up ready to edit file onsys. At which point, Mary would enter these lines to create her script:

# custom who script – mary – 10 Apr 11

date

echo “Users currently logged into the system:”

who

# end of script

Here’s what Mary actually did… she made a comment in the first line that begins with the character #, so that the shell knows to ignore that line. In the next line, she sets the command “date” so the script will output the date along with whatever else she requests it to do. In line 4, she uses the built-in echo command to tell the shell to display whatever is being asked for following the echo command. In this case, Mary wants the script to display Users currently logged into the system: when she runs it. The next command that Mary enters into this little script is the built-in who command. And lastly, is her notation that the script has ended.

Now, to make this script work, Mary needs to make it executable. In other words, she has to change the file’s permissions to allow herself (and other, if she wants) to execute (make it run) the script onsys. She will do that this way:

mary@workplace:~$ chmod +x onsys

If she now listed the permissions for this file, they would look like this:

mary@workplace:~$ ls -l onsys

-rwxr-xr-x 1 mary users 94 Apr 10 15:21 onsys

What this means is that everyone on the system can read and execute this script, but only mary can write to (change) it.

OK, so Mary wants to test her script now, so she just types in the script name at the command line (assuming the script is in her working directory):

mary@workplace:~$ onsys

Sun Apr 10 15:48:09 EDT 2011

Users currently logged into the system:

bill           pts/4       April 10 09:12

james       pts/5       April 10 09:30

marjory    pts/11     April 10 10:02

ken           pts/16     April 10 10:31

And so, there you have it. Mary wrote a simple script using shell built-in commands to perform a function that she does repetitively every day. That’s what scripts do. They perform tasks for us at our bidding. Of course, scripts can get much more complicated; so complicated in fact as to be considered applications in their own right. A script is just a series of encoded commands and variables designed to do something, which is basically what an application is also.

Start yourself a directory in your /home directory where you can create and play around with scripting and scripts. It can really be fun. You can “automate” a lot of every day stuff that you do on your computer using custom-made scripts. Yes, you can do it. It ain’t rocket science. It is similar to the technology used to program rockets, though.

Have FUN!

~Eric


An Introduction to the Linux Shell

She sells seashells by the seashore. Well, yes… that may be true, but that’s not the type of shell we’re going to talk about here today.

I’m going to talk a bit about the Linux shell. What is the Linux shell? What does it do? How can I interact with it on my GNU/Linux operating system? Those are all good questions. While today’s most popular distributions of GNU/Linux are morphing into operating systems that are more and more graphic user interface oriented, the real power of Linux still resides in the command line.

When you boot up your Ubuntu or Mandriva GNU/Linux operating systems, most of you see a graphical login screen. Others, like myself, might see a non-graphic command line login. Both do basically the same thing. They log the user into the Linux shell so that he may begin to utilize his system’s potential to perform tasks. That’s what we do with our computers, regardless of what the tasks happen to be… emailing pics to auntie Myrtle or hacking cloud and cluster security systems.

The Linux shell is the interface between you in that seat in front of your monitor and the operating system that controls the hardware in that box under the desk that does the actual stuff you want done. There are numerous shells in Linux; the most commonly used one is called BASH – Bourne Again Shell. You’re in the shell anytime you’re logged into your GNU/Linux operating system; whether you’re interacting with it graphically or from the command line.

Graphic User Interfaces or GUIs are just “front ends” to applications that are running in the shell. I’ll be talking mostly about the non-graphical command line interface here today, though. You can access your command line interface from within your GUI by using the graphic front end application for the command line provided by your desktop environment. For example, in Gnome, you could use Gnome Terminal; or in KDE, you could use Konsole. Either way, these are both just graphical front ends for the BASH shell command line.

When you first login, you’ll get what’s known as a prompt. It is just a blank line waiting for your input (commands). It’ll look something like this:

joe@mysystem:~$

The first part, “joe”, is just the user’s login name. “@mysystem” is the name of the computer the user is logged into. The “~” character tells us that user Joe is working from his home directory. The “$” character is the standard character denoting a non-root, regular user.

Let’s say Joe wants to list all the files in his home directory. All he has to do is type:

joe@mysystem:~$ ls

This command, known as “list”, tells the shell that user Joe wants to see a list of all the contents of his home directory. The shell immediately responds after Joe hits the Return (Enter) key on his keyboard with this output:

Desktop  joe_archives  joe_common     joe_private
Dropbox  joe_backups   joe_downloads

It looks like Joe has five regular directories, a Dropbox directory, and the directory that contains his desktop icons. In reality, there are more directories and files in Joe’s home directory, but they’re what are known as “hidden” files. Their names are usually preceded by a .(period) to make them hidden. If joe wants to see all his directories and files he can list them this way:

joe@mysystem:~$ ls -a

The “-a” option means all. The list command will list all items in a directory when using the -a option. Joe’s list now looks something like this:

.dropbox        .macromedia      .thunderbird
.ICEauthority           .esd_auth    .moz_icons      .viminfo
.PySolFC           .fontconfig    .mozilla      .wicd
.Xauthority           .gconf        .mozilla_3.x      .xchat2

The above directories are hidden by the preceding .(period), as mentioned above.

Let’s say Joe want’s to create a grocery list for his afternoon shopping chores. He can do this via the shell and command line also by using a command line editor such as vim. He would first do this by bring up the vim application in the command line interface:

joe@mysystem:~$ vim groceries

This command would initiate the vim application using a new file called “groceries”. Vim or Vi-Improved, as it’s known, is a non-graphical text editing application. It would look something like this to Joe:

lettuce

tomatoes

catfood

peanut butter

eggs

milk

bread

~

~

~

–INSERT–                                                 10,1          All

Once Joe had finished typing out his grocery list, he would save it using the vim command :wq, which would also close the vim application and bring Joe back to the command line prompt. He could also print his list from the command line like this:

joe@mysystem:~$ lpr groceries

The lpr command would tell the shell that Joe wants to output the contents of the groceries file to the printer. The printer would receive the data and the command to print from the computer’s hardware and begin printing Joe’s grocery list.

All of this we’ve talked about today doesn’t even scratch the surface of the power at your finger tips when using the Linux shell. Your first step should be to read the Linux manual page for the BASH shell. There is some very useful information in that document. Stay tuned here… I’ll come up with some other lessons in the future. Remember what I always say…

Learn something. It won’t hurt you none. I promise.

Later…

~Eric

*This was previously posted on my old Nocturnal Slacker (@LockerGnome) blog and on the Community Blogs at Linux.com. I’m reposting it here to keep the series all in one place.


Follow

Get every new post delivered to your Inbox.

Join 37 other followers