dcsimg

Command Line Magic: Scripting, Part One

Creating shell scripts is the lazy (smart) system administrator's way to automate and build intelligence into repetitive tasks.

This scripting miniseries will help you learn some basic scripting and perhaps launch you on a more productive career in Linux system administration. If you find yourself typing the same commands over and over again, do yourself a favor and write a script to do it. Smart administrators have scripts for everything. There are three things you have to remember about scripting: Don’t test your scripts on production systems, document the script’s action, and name the script descriptively. This week’s article introduces you to simple shell scripting.

There’s no great secret to creating good scripts. You don’t have to be afraid of criticism about script security, programming style, or which editor you use to create them. You don’t need to be a programmer or even a Linux expert to create some pretty cool scripts. All you really must do is test your scripts, document your scripts, and give your scripts descriptive names.

Test your scripts to make sure they work like you expect them to. Document your scripts inside and outside the script so that you don’t forget what the script’s original purpose is and that anyone else using the script will know how to use it. Name your scripts such that its functionality is at least hinted at from the name.

Those of you who are new to Linux might ask, “Why do I need to create scripts?” The answer is simple, you don’t need to but once you’re worn out from typing the same commands several times per day, you’ll want to create them.

Scripts help you manage your system(s) more efficiently. And, the rule of thumb is, that if you’re typing it, you can script it.

The Basics

I only use the Bourne-Again Shell (BASH)* for scripting. It’s the default shell in Linux and it has the best features from the other major shells. But don’t worry if you don’t use BASH, you can still write scripts that use BASH, even if you use a different user shell. The prerequisites for gaining the most out of this series are that you know how to use basic shell commands. For example, cd, ls, ps, cat, chmod and a command line text editor such as vi, emacs, or nano.

I only use the vi** editor in the examples.

This series is interactive, so you’ll need access to a Linux system, physical or virtual, with which to work. If you’re required to use root user privileges or sudo access, I’ll note that in the example.

At the Command Line

To keep my scripts in this separate from everything else, I created an area in my home directory for them under scripts/intro. Shell scripts are simply commands contained in a file (a shell script file) that run as if you had run them individually at the command line. Enough introduction, let’s get started with some shell scripts.

Open your favorite text editor and enter some commands into it on a one command per line basis. To run the script within a BASH shell, enter #!/bin/bash.

#!/bin/bash

This line, which must be the first line in your script, is the only line that may begin with the # (shell) symbol. Placed anywhere else in the script and the shell interpreter ignores it and anything on the line after it. This is the comment symbol everywhere but on the first line. Continue by adding some commands as shown.

#!/bin/bash
touch file.txt
echo "Hello World" >> file.txt
echo "Create another file" > file2.txt
ls -la
cat file.txt
echo " "
cat file2.txt

Save the file with a descriptive name (no spaces) and with a .sh at the end of the name. For example, file_create.sh. The file is simply a text file, not a script at this point. Do you know why? It’s because the file permissions don’t allow execution. To change the behavior, enter the following command:

$ chmod +x file_create.sh

Now, execute the file and allow it to create the two files and display their contents.

./file_create.sh

total 20
drwxr-xr-x 2 khess khess 4096 2011-05-30 16:01 .
drwxr-xr-x 3 khess khess 4096 2011-05-30 15:35 ..
-rw-r--r-- 1 khess khess   20 2011-05-30 16:01 file2.txt
-rwxr-xr-x 1 khess khess  141 2011-05-30 15:55 file_create.sh
-rw-r--r-- 1 khess khess   12 2011-05-30 16:01 file.txt
Hello World

Create another file

As demonstrated in the script, you can enter as many commands as you want into a script. They will execute one after the other, from top to bottom, until the script completes.

For a practical application of the script you created, try this one that checks the lastlog and then mails you the list of user who’ve logged in during the current month.

#!/bin/bash

echo $HOSTNAME > logins.txt
echo " " >> logins.txt
last >> logins.txt
mail ken@kenhess.com < logins.txt

Note that this file records the hostname to help you identify which hosts your messages originate from, in case you have more than one that you manage.

Interactive Scripting

What if you need more flexibility in your scripts that you just can't program to run autonomously? You program them to run interactively and provide the intelligence as needs and circumstances change.

This script prompts you for a filename, some initial content for the file, and then writes the provided information to the specified filename.

#!/bin/bash

echo "Enter the name of the file you wish to create."
read FILENAME
echo "Enter any info you want to enter into the file."
read INFO
touch $FILENAME
echo $INFO >> $FILENAME
cat $FILENAME

Grant execute permission to the file and run it. Does it produce the expected results? Do you see any other useful applications for the scripts you've learned today? How about interactively adding information into a database?

Next week, you'll gain considerably more experience writing scripts now that all of the trivia and introductory material is out of the way. If you have particular problems you'd like to solve, use the Comments section to address them. Other readers might have some good ideas or I can tackle them in upcoming posts. Try out a few scripts on your own from the ones you've learned here and post them for others to use.

Remember, that we're an interactive, positive community that wants to foster open communications. Everyone comes in from a different place and it's our job to nurture learning and discovery. Until next week, happy scripting!

* Watch for my upcoming, Korn Shell tale post.

** It's the first one I learned from old UNIX nerds who thought that vi is the only editor. What can I say?

Comments on "Command Line Magic: Scripting, Part One"

dragonwisard

“I only use the vi** editor in the examples.”
I don’t see anywhere in the examples where you make any reference to vi… so why even bring this up? You’re gonna start a holy war for nothing.

Also, shouldn’t you explain a bit about the syntax and operators you’re using or do you expect that the reader is already an expert at BASH?

You should explain > vs >> and when to use $. I see a lot of noobs get frustrated over $ in their first scripts.

Reply
subramanyam

It would be better if you differentiate between # and $. I thought it will be useful for the people who are familiar with Shell scripting. If it for the first time users you should explain the syntax and the operators .And also you should tell what are the basic commands which will be helpful while writing shell scripting.

Reply
    srinu.matta5

    # is used for comment the line except the line #!/bin/sh
    $ is a value substitution. if we write $variable then it gives value of the variable.

    Reply
pyeniemi

Why do you give such silly examples (like the interactive…)?
No need to “touch” the file, when you will simply append $INFO to it.

Reply

    “touch” allows your script to error out in case the directory is read-only for your user rather than waiting for the user to enter some text then find out it errored out after a long input.

    Reply
doricman

I keep Richard Blum’s “Linux Command Line and Shell Scripting Bible” handy any time I need “Command Line Magic”.
The Bible is a great investment for a refresh and helping hand.

Reply
hardy59

Come on, this is sort of a scripting appetizer which might attract Linux newbies to get themselves a book or to read some online tutorial on shell scripting. Also I wouldn’t start with syntax and rules and stuff for exactly that reason. And finally: Linux newbies will never start a holy war on editors, this is the domain of smartarses.

Reply
goarilla

I think this was mostly useless.
No offense but a few points:

- you make it very clear that this is for bash
yet your title makes it seem like it’s sh scripting,
which is i think something you should do: you should teach
generic shell scripting, common idioma’s, printf instead of echo, posix, etc …

- you start by giving the moronic hell world example;
I can tolerate that if it’s for a language that needs a buildchain
and where compiling and running is necessary eg: C*, java, haskell, … but please not for shell.
But then you continue to a pseudo-useful example and call it a day. It’s the second example that wets the beaks of the new ones even though
they probably have never heard about the last command. That’s wrong imho give them a few more.

Reply
rrohbeck

Everything you need to know about bash scripting (I still use it as a reference after many years):
http://tldp.org/LDP/abs/html/index.html
It’s not a gentle introduction though.

Reply

    Well, for this article’s target audience, maybe this one would be a better place to start,
    http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html

    Good call linking to tldp (The Linux Documentation Project http://tldp.org/ ). It’s been rather a while since I checked it out — and it looks like the material is now being updated more regularly (for a while it was kind of lagging). I highly recommend it. It’s a really great site for anyone able to study on their own, from newbie to guru.

    Reply

      What is the error you are gitteng? Is the initial mysql connection succeeding?Edit: I managed to catch your errors. Here's what you're gitteng -Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/w/e/e/weeklydraft/html/register.php on line 15Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/w/e/e/weeklydraft/html/register.php on line 16Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/w/e/e/weeklydraft/html/register.php on line 27Take a look at the code at those line numbers, and make sure the connection variable you are passing to the mysql_* functions is the same as the variable you are setting in database.php.

      Reply
hennuz

*ahem* (perl) *cough* – going back to square one…(sound of throat clearing)

Reply
maximilianh

This is not a great introduction into bash scripting. I don’t see any sense in this article.

Reply
khess

I’ll bet that if I gave away solid gold coins, some people would have a problem with them not being silver. I don’t have enough money to test that theory.

Reply
raultman

Khess, don’t let the other comments phase you. Everyone these days thinks they are better than everyone else. Keep doig what you are doing…

Reply
    turbopro

    Khess, please continue with the program. There shall be those of us who will benefit from and make good use of the knowledge that you share with us here.

    merci beaucoup

    Reply
mtabholt

Khess — Wow! Sounds like lots of people missed their morning coffee! :)
Good tutorial, and great starting point for people who are not familiar with scripting. Keep it up!

Reply
rocteur

This article is called command line magic so I expected to find magic but it is a tutorial for people who have never tried to write a script. The article should be renamed….

Reply
mikemeek

It’s PART ONE, PEOPLE! Let the man talk before you begin ranting about omissions. How many things can you include in a first article?

And please, no condescending messages, the man’s trying to help (which he does.) Those of you dissatisfied with the article don’t need it.

Excellent piece, as usual, Ken. Keep ‘em coming.

Reply
wayneBC

Excellent piece, I’m new to scripting and this has taken out all the doubt i had about it being worthwhile. Keep up the good work

Reply
Cookie_Monstr

Its a start so lets keep it up.

Reply