Quick Guide To Bash Scripts PDF
Quick Guide To Bash Scripts PDF
html
A shell script is little more than a list of commands that are run in sequence. Conventionally,
a shellscript should start with a line such as the following:
#!/bin/bash
THis indicates that the script should be run in the bash shell regardless of which interactive
shell the user has chosen. This is very important, since the syntax of different shells can
vary greatly.
A simple example
Here's a very simple example of a shell script. It just runs a few simple commands
#!/bin/bash
echo "hello, $USER. I wish to list some files of yours"
echo "listing files in the current directory, $PWD"
ls # list files
Firstly, notice the comment on line 4. In a bash script, anything following a pound sign #
(besides the shell name on the first line) is treated as a comment. ie the shell ignores it. It is
there for the benifit of people reading the script.
$USER and $PWD are variables. These are standard variables defined by the bash shell
itself, they needn't be defined in the script. Note that the variables are expanded when the
variable name is inside double quotes. Expanded is a very appropriate word: the shell
basically sees the string $USER and replaces it with the variable's value then executes the
command.
Variables
X="hello"
$X
More specifically, $X is used to denote the value of the variable X. Some things to take note
of regarding semantics:
bash gets unhappy if you leave a space on either side of the = sign. For example, the
following gives an error message:
1 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
X = hello
while I have quotes in my example, they are not always necessary. where you need
quotes is when your variable names include spaces. For example,
This is because the shell essentially sees the command line as a pile of commands and
command arguments seperated by spaces. foo=baris considered a command. The problem
with foo = bar is the shell sees the word foo seperated by spaces and interprets it as a
command. Likewise, the problem with the command X=hello world is that the shell interprets
X=hello as a command, and the word "world" does not make any sense (since the assignment
command doesn't take arguments).
Basically, variable names are exapnded within double quotes, but not single quotes. If you
do not need to refer to variables, single quotes are good to use as the results are more
predictable.
An example
#!/bin/bash
echo -n '$USER=' # -n option stops echo from breaking the line
echo "$USER"
echo "\$USER=$USER" # this does the same thing as the first two lines
$USER=elflord
$USER=elflord
so the double quotes still have a work around. Double quotes are more flexible, but less
predictable. Given the choice between single quotes and double quotes, use single quotes.
Sometimes, it is a good idea to protect variable names in double quotes. This is usually the
most important if your variables value either (a) contains spaces or (b) is the empty string.
An example is as follows:
#!/bin/bash
X=""
if [ -n $X ]; then # -n tests to see if the argument is non empty
echo "the variable X is not the empty string"
fi
2 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
Why ? because the shell expands $X to the empty string. The expression [ -n ] returns true
(since it is not provided with an argument). A better script would have been:
#!/bin/bash
X=""
if [ -n "$X" ]; then # -n tests to see if the argument is non empty
echo "the variable X is not the empty string"
fi
In this example, the expression expands to [ -n "" ] which returns false, since the string
enclosed in inverted commas is clearly empty.
Just to convince you that the shell really does "expand" variables in the sense I mentioned
before, here is an example:
#!/bin/bash
LS="ls"
LS_FLAGS="-al"
This looks a little enigmatic. What happens with the last line is that it actually executes the
command
ls -al /home/elflord
(assuming that /home/elflord is your home directory). That is, the shell simply replaces the
variables with their values, and then executes the command.
OK. Here's a potential problem situation. Suppose you want to echo the value of the variable
X, followed immediately by the letters "abc". Question: how do you do this ? Let's have a try :
#!/bin/bash
X=ABC
echo "$Xabc"
THis gives no output. What went wrong ? The answer is that the shell thought that we were
asking for the variable Xabc, which is uninitialised. The way to deal with this is to put
braces around X to seperate it from the other characters. The following gives the desired
result:
#!/bin/bash
X=ABC
echo "${X}abc"
Conditionals, if/then/elif
3 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
Sometimes, it's necessary to check for certain conditions. Does a string have 0 length ? does
the file "foo" exist, and is it a symbolic link , or a real file ? Firstly, we use the if command to
run a test. The syntax is as follows:
if condition
then
statement1
statement2
..........
fi
Sometimes, you may wish to specify an alternate action when the condition fails. Here's
how it's done.
if condition
then
statement1
statement2
..........
else
statement3
fi
alternatively, it is possible to test for another condition if the first "if" fails. Note that any
number of elifs can be added.
if condition1
then
statement1
statement2
..........
elif condition2
then
statement3
statement4
........
elif condition3
then
statement5
statement6
........
fi
The statements inside the block between if/elif and the next elif or fi are executed if the
corresponding condition is true. Actually, any command can go in place of the conditions,
and the block will be executed if and only if the command returns an exit status of 0 (in
other words, if the command exits "succesfully" ). However, in the course of this document,
we will be only interested in using "test" or "[ ]" to evaluate conditions.
The command used in conditionals nearly all the time is the test command. Test returns true
or false (more accurately, exits with 0 or non zero status) depending respectively on
4 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
for some tests, there need be only one operand (operand2) The test command is typically
abbreviated in this form:
#!/bin/bash
X=3
Y=4
empty_string=""
if [ $X -lt $Y ] # is $X less than $Y ?
then
echo "\$X=${X}, which is smaller than \$Y=${Y}"
fi
if [ -n "$empty_string" ]; then
echo "empty string is non_empty"
fi
if [ 1=2 ]; then
echo "hello"
fi
gives exactly the "wrong" output (ie it echos "hello", since it sees an operand but no
operator.)
Another potential trap comes from not protecting variables in quotes. We have already
given an example as to why you must wrap anything you wish to use for a -n test with
quotes. However, there are a lot of good reasons for using quotes all the time, or almost all
of the time. Failing to do this when you have variables expanded inside tests can result in
very wierd bugs. Here's an example: For example,
5 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
#!/bin/bash
X="-n"
Y=""
if [ $X = $Y ] ; then
echo "X=Y"
fi
This will give misleading output since the shell expands our expression to
[ -n = ]
Here's a quick list of test operators. It's by no means comprehensive, but its likely to be all
you'll need to remember (if you need anything else, you can always check the bash manpage
... )
number of
operator produces true if...
operands
!= opposite of = 2
Loops
6 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
Loops are constructions that enable one to reiterate a procedure or perform the same
procedure on several different items. There are the following kinds of loops available in
bash
for loops
while loops
For loops
#!/bin/bash
for X in red green blue
do
echo $X
done
THe for loop iterates the loop over the space seperated items. Note that if some of the items
have embedded spaces, you need to protect them with quotes. Here's an example:
#!/bin/bash
colour1="red"
colour2="light blue"
colour3="dark green"
for X in "$colour1" $colour2" $colour3"
do
echo $X
done
Can you guess what would happen if we left out the quotes in the for statement ? This
indicates that variable names should be protected with quotes unless you are pretty sure
that they do not contain any spaces.
The shell expands a string containing a * to all filenames that "match". A filename matches if
and only if it is identical to the match string after replacing the stars * with arbitrary strings.
For example, the character "*" by itself expands to a space seperated list of all files in the
working directory (excluding those that start with a dot "." ) So
echo *
echo *.jpg
echo ${HOME}/public_html/*.jpg
As it happens, this turns out to be very useful for performing operations on the files in a
7 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
#!/bin/bash
for X in *.html
do
grep -L '<UL>' "$X"
done
While Loops
#!/bin/bash
X=0
while [ $X -le 20 ]
do
echo $X
X=$((X+1))
done
This raises a natural question: why doesn't bash allow the C like for loops
As it happens, this is discouraged for a reason: bash is an interpreted language, and a rather
slow one for that matter. For this reason, heavy iteration is discouraged.
Command Substitution
Command Substitution is a very handy feature of the bash shell. It enables you to take the
output of a command and treat it as though it was written on the command line. For
example, if you want to set the variable X to the output of a command, the way you do this is
via command substitution.
There are two means of command substitution: brace expansion and backtick expansion.
Brace expansion workls as follows: $(commands) expands to the output of commands This
permits nesting, so commands can include brace expansions
An example is given;:
#!/bin/bash
files="$(ls)"
web_files=`ls public_html`
echo "$files" # we need the quotes to preserve embedded newlines in $files
echo "$web_files" # we need the quotes to preserve newlines
X=`expr 3 \* 2 + 4` # expr evaluate arithmatic expressions. man expr for details.
echo "$X"
The advantage of the $() substitution method is almost self evident: it is very easy to nest. It
is supported by most of the bourne shell varients (the POSIX shell or better is OK). However,
8 of 9 04/02/2013 03:57 PM
http://www.panix.com/~elflord/unix/bash-tute.html
the backtick substitution is slightly more readable, and is supported by even the most basic
shells (any #!/bin/sh version is just fine)
Note that if strings are not quote-protected in the above echo statement, new lines are
replaced by spaces in the output.
9 of 9 04/02/2013 03:57 PM