|
|
Shell Scripts Seem Like Magic at First
Linux Tips by Burleson Consulting |
Programming with Linux
In this chapter, additional Linux commands will be
introduced and used to show how to build programs called shell scripts. Shell
scripting is the topic of my next book, Oracle Shell Scripting: Linux and UNIX
Programming for Oracle in which I cover much of this information in great
detail. If you are interested in shell scripting you might want to pick it up!
Though some of the examples use Oracle, much of the books content is applicable
for anyone interested in the topic. Here we will just cover things briefly in
order to give a general idea about how scripts are created to perform functions
that are repeated on a periodic basis.
Instead of jumping directly into the creation of
scripts, there are a few additional topics that should be presented first as
well as some questions that need to be answered.
What is a shell?
As described earlier in this book a shell provides a
command interpreter environment for giving instructions to the Linux kernel via
English-like commands. There are a number of different shell environments
available in Linux. In order to determine which shell environments are
available on a user?s version of Linux, the following command should be issued:
$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/bash2
/bin/ash
/bin/bsh
/bin/ksh
/bin/tcsh
/bin/csh
/bin/zsh
The resulting list indicates that there are several
shells available on this server such as the Bourne shell (sh), the Bourne Again
shell (bash), the Korn shell (ksh) and the C-shell (csh), among others. In
Linux, the bash shell is normally the default shell.
To determine the default shell to which the system is
currently set, the following command should be run:
$ echo $SHELL
/bin/bash
As predicted, the bash shell is the current default.
The shell is a command language interpreter that
executes commands read from the standard input device such as a keyboard or from
a plain text file.
A shell can be compared to the Microsoft Windows command
prompt, which also executes commands issued from the keyboard or from text
(.bat) files; however, the shell environment is much more robust and
feature-rich than its Windows counterpart.
Command Aliases
Aliases are the shell scripts little cousin. A shell
script, as we will see shortly, is a file which contain a list of commands.
Aliases, in contrast, can be used to perform complex commands but are set in the
shell like environmental variables.
If you find yourself frequently using a fairly
complicated command like the one below and want to be able to run it without
typing the whole thing you can use the alias command to set up a shortcut.
$ alias alert=?tail -200 /u01/installed/software/mypgm/alert.log|more?
Now you can execute this whole command simply by typing
alert. Many of the features we will discuss when talking about shell scripting
are available in aliases including the pipe (|) used to send the output of one
command to the input of another.
A small set of well written aliases can save you a lot
of keystrokes, just be careful not to make aliases with the same name as
commands you use unless you want the alias to replace that command when you type
it.
Not just any clown can learn Linux shell scripts
Why use shell scripts?
Since the shell is capable of reading commands from a
file, called a script, it becomes a simple task to use a basic text editor like
vi to build scripts to simplify frequently performed tasks and to automate the
performance of those tasks.
Programming simple or complex actions into shell scripts
can simplify repetitive administrative and maintenance functions within the
Linux environment.
Getting Started with a Simple Shell Script
A shell script gets executed just as if you were at the
keyboard typing commands. Because of this we will demonstrate many of the
capabilities of shell scripts right at the command line. Once tested at the
command line, these commands can be moved into shell scripts. Here's an example
of a simple shell script:
#!/bin/bash
# A simple shell script to output the time
echo "The current date and time is `date`"
This three line script starts with two comments
(anything following a # is ignored by the shell), then has an echo command which
outputs some text and the current date with the date command.
If you enter these lines into a text file called
what_time.sh youl can then execute it as a shell script with the bash command:
$ bash what_time.sh
The current date and time is Fri Sep 15 14:58:23 EDT
2006
The .sh extensions is not necessary but is the
conventional way to indicate a bash shell script.
If we want to make things even easier we can change the
execution privileges on this shell script so we don't even need the bash command
to run it.
$ chmod u+x what_time.sh
$ ./what_time.sh
The current date and time is Fri Sep 15 15:18:56 EDT
2006
This works because the first line of our what_time.sh
shell script is special. When the first line of a shell script begins with #!
it is used as a hint to indicate what command should be used to execute this
script. When Linux processes this script for execution it sees this hint and
uses bash to execute it.
We'll spend the rest of this chapter looking at many of
the hundreds of commands you can use in shell scripts, but remember, shell
scripts don't have to be complicated! They may just take the place of a couple
commands that you run frequently or a long command that you have troubles
remembering.
Shell variables
Shells provide an abundance of useful built-in
information that can be referenced in globally available variables. In order to
see the information provided in a shell, the set command can be run as
demonstrated below.
Here's a partial output of the set command:
$ set
BASH=/bin/bash
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1"
[4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='2.05b.0(1)-release'
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/tclark/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/tclark
HOSTNAME=appsvr.mytec.com
OSTYPE=linux-gnu
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/tclark/bin
...
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/home/tclark
SHELL=/bin/bash
SHLVL=1
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_CLIENT='206.107.231.178 1379 22'
SSH_CONNECTION='206.107.231.178 1379 192.168.15.105 22'
SSH_TTY=/dev/pts/0
SUPPORTED=en_US.UTF-8:en_US:en
TERM=vt100
UID=503
USER=tclark
_=clear
The contents of a shell variable can be displayed by
using the echo command and prefacing the variable name with a dollar sign as
demonstrated below. Shell variables are referenced using all capital letters.
$ echo $TERM
vt100
$ echo $USER
tclark
$ echo $HOSTNAME ... $LOGNAME
appsvr.mytec.com ... tclark
There are also some special built-in variables that can
be useful when creating shell scripts. Some of them are listed in Table 8.1
below.
Built-in Variable |
Description |
$# |
The total number of arguments passed
to a shell script on the command line. |
$* |
All arguments passed to the shell
script. |
$0 |
The command (script) invoked on the
command line. |
$1 - $9 |
The first through ninth arguments
passed to the shell script from the command line. |
Table 8.1: Shell built-in variables
These variables are provided by the shell, but when
programming shell scripts you may need additional variables. Next we'll look at
variables you can make yourself, user defined variables.
User Defined Variables
Shells also allow the creation of variables for use
within scripts (local variables) and for passing between scripts (global
variables). User variables are traditionally created using lower-case
characters though they can be any case you want.
The creation of a variable requires merely choosing a
lower-case name for the variable and giving it a value using an equal (=) sign.
There should be no spaces on either side of the equal sign. If there are spaces
or special characters in the contents of your variable you should enclose the
variable in single quotes.
The unset command can be used to nullify the value of a
previous set variable. The following are some examples:
$ myname='Terry Clark'
$ echo myname
myname
$ echo $myname
Terry Clark
$ unset myname
$ echo $myname
$ number=10
$ echo $number
10
In this example we see that in order to set the contents
of a variable we just give the variable name, but if we want to retrieve the
contents of a variable we must use a dollar sign ($) before the variable name.
The variables created above are local variables
available only to the current shell. Variables must be exported if they are to
become global so they can be referenced by other shell scripts. The following
example shows the creation of global user variables.
$ myname=Terry
$ export myname
In the bash shell we can combine these two lines into
one and use the following command to set and export a variable:
$ export myname=Terry
Variables are great for storing information. Later
we'll see how we can use variables to make decisions and loops, but first we've
got a little more basic ground to cover.
Evaluating Expressions
Shells can evaluate complex expressions and allow the
user to perform math functions. The following examples demonstrate some
simplistic math using the expr command:
$ expr 6 + 3
9
$ expr 6 + 3 + 8
17
$ expr 8 - 2
6
$ expr 12 / 3
4
$ expr 15 \* 3
45
$ expr 20 % 3
2
$ echo `expr 15 + 3`
18
The example above shows not only the common use of expr
but some of the exceptions you may find in the expr command, such as:
The multiplication example uses an escape character (\)
before the * so that the * is not interpreted as a wildcard symbol.
?20 % 3? is read as 20 mod 3 and yields the remainder
when 20 is divided by 3.
The last example uses backward quote signs (`) to
enclose the command. The command within the backward quotes is executed and the
result echoed to the screen.
This is an excerpt from "Easy
Linux Commands" by Linux guru Jon Emmons. You can purchase it for only
$19.95 (30%-off) at
this link.