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


9 Comments on “Beginning Shell Scripting”

  1. Very nice.

    I started thinking of the ways to automate such a task through cron and pipe it to a file that could be tailed in another term. LOL!

  2. neil says:

    This is great! I want to learn to write scripts. I’ve found sites about bash scripting but even the basic ones assume you have had some experience with writing them or knowing the lingo…. I’ve mostly cut and paste ones I’ve found but am usually stuck with what they wrote not knowing enough to customize it just the way I would like it to work. Will you be doing more on basic scripting?

    • Hi Neil!

      Welcome here!

      In answer to your question… yes. I will be writing some more shell scripting tutorials. They’ll be geared toward the absolute beginner, mostly though. Know this… I’m no GNU/Linux guru. I have a lot to learn myself. I do enjoy passing what I have learned onto others.

      Stay tuned… and thanks for reading/commenting. 🙂

      ~Eric

  3. […] Beginning Shell Scripting 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. […]

  4. […] man copyright Beginning Shell Scripting […]

  5. Edy says:

    Hi, Eric

    Your site style is very cool. wish you could write more cool articles.

    • Thanks very much, Edy. Stay tuned… I change themes often. I like variety. As far as writing more articles… well, I’m trying.

      By the way, bookmarked you site. Very good info there.

      Regards,

      ~Eric

  6. קורס הזרקות בוטוקס…

    … בוטוקס השוואת מחירים – השרירים המשותקים לא יכולים לנוע וליצור את הקמט אותו רוצים להעלים ובכך משתק אותם וגורם להם להיות בוטוקס למניעת זיעה. זאת, בטיפולים אסתטיים ריכוז החומר של הבוטולניום טוקסין למספר … Beginning Shell Scripting « Nocturnal Slacke…


Leave a reply to Links 11/4/2011: CentOS 5.6, Fedora 15 RC1 | Techrights Cancel reply