Creating Simple Shell Scripts
Shell scripts are good for automating repetitive shell tasks. Bash and other shells include the basic constructs found in various programming languages, such as loops, tests, case statements, and so on. The main difference is that there is only one type of variable: strings.
Editing and Running a Script
Shell scripts are simple text files. You can create them using your favorite text editor (such as vi). To run, the shell script file must be executable. For example, if you created a shell script with a file name of myscript.sh
, you could
make it executable
as follows:
$
chmod u+x myscript.sh
Also, the first line of your bash scripts should always be the following:
#!/bin/bash
As with any command, besides being executable the shell script you create must also either be in your PATH or be identified by its full or relative path when you run it. In other words, if you just try to run your script, you may get the following result:
$
bash: myscript.sh: command not found
In this example, the directory containing
myscript.sh
is not included in your PATH.
To correct this problem, you can edit your path, copy the script to a directory in your PATH, or enter the full or relative path to your script. Those four examples, respectively, are shown below:
$
mkdir ~/bin ; cp myscript.sh ~/bin/ ; PATH=$PATH:~/bin
$
cp myscript.sh /usr/local/bin
$
./myscript.sh
$
/tmp/myscript.sh
Avoid putting a dot (.) into the PATH to indicate that commands can be run from the current directory. This is a technique that could result in commands with the same file name as important, well-known commands (such as
ls
or
cat
), which could be over-
ridden if a command of the same name exists in the current directory.
Adding Content to Your Script
Although a shell script can be a simple sequence of commands, shell scripts can also be used as you would any programming language. For example, a script can produce different results based on giving it different input. This section describes how to use compound commands, such as
if/then
statements,
case
statements, and
for/while
loops in your shell scripts.
The following example code assigns the string
abc
to the variable
MYSTRING
. It then
tests the input to see if it equals abc and acts based on the outcome of the test. The test is what takes place between the brackets (
[ ]
):
MYSTRING=abc
if [ $MYSTRING = abc ] ; then
echo “The variable is abc”
fi
To
negate the test
, use
!=
instead of
=
as shown in the following:
if [ $MYSTRING != abc ] ; then
echo “$MYSTRING is not abc”;
fi
The following are examples of
testing for numbers
:
MYNUMBER=1
if [ $MYNUMBER -eq 1 ] ; then echo “MYNUMBER equals 1”; fi
if [ $MYNUMBER -lt 2 ] ; then echo “MYNUMBER <2”; fi
if [ $MYNUMBER -le 1 ] ; then echo “MYNUMBER <=1”; fi
if [ $MYNUMBER -gt 0 ] ; then echo “MYNUMBER >0”; fi
if [ $MYNUMBER -ge 1 ] ; then echo “MYNUMBER >=1”; fi
Let’s look at some
tests on file names
. In this example, you can check if a file exists
(-e
),
if it’s a regular file (
-f
), or if it is a directory (
-d
). These checks are done with
if
/
then
statements. If there is no match, then the
else
statement is used to produce the result.
filename=”$HOME”
if [ -e $filename ] ; then echo “$filename exists”; fi
if [ -f “$filename” ] ; then
echo “$filename is a regular file”
elif [ -d “$filename” ] ; then
echo “$filename is a directory”
else
echo “I have no idea what $filename is”
fi
Table 3-1 shows examples of tests you can perform on files, strings, and variables.
No comments:
Post a Comment