-CONTINUED
VARIABLES
Variables are basically "boxes" that hold values. You will
want to create variables for many reasons. You will need
it to hold user input, arguments, or numerical values. Take
for instance the following piece of code:
#!/bin/bash
x=12
echo "The value of variable x is $x"
What you have done here, is to give x the
value of 12 . The line echo "The value
of variable x is $x" prints the current value of
x . When you define a variable, it must not
have any whitespace in between the assignment operator:
"= ". Here is the syntax:
variable_name=this_value
The values of variables can be accessed by prefixing the
variable name with a dollar symbol: "$ ". As
in the above, we access the value of x by using
echo $x .
There are two types of variables. Local variables, and
environmental variables. Environmental variables are set
by the system and can usually be found by using the env
command. Environmental variables hold special values. For
instance, if you type:
xconsole$ echo $SHELL
/bin/bash
You get the name of the shell you are currently running.
Environmental variables are defined in /etc/profile
and ~/.bash_profile . The echo
command is good for checking the current value of a variable,
environmental, or local. If you are still having problems
understanding why we need variables, here is a good example:
#!/bin/bash
echo "The value of x is 12."
echo "I have 12 pencils."
echo "He told me that the value of x is 12."
echo "I am 12 years old."
echo "How come the value of x is 12?"
Okay, now suppose you decide that you want the value of
x to be 8 instead of 12. What do you do? You
have to change all the lines of code where it says that
x is 12. But wait... there are other lines
of code with the number 12. Should you change those too?
No, because they are not associated with x .
Confusing right? Now, here is the same example, only it
is using variables:
#!/bin/bash
x=12 # assign the value 12 to variable x
echo "The value of x is $x."
echo "I have 12 pencils."
echo "He told me that the value of x is $x."
echo "I am 12 years old." echo "How come the value of x
is $x?"
Here, we see that $x will print the current
value of variable x , which is 12. So now, if
you wanted to change the value of x to 8, all
you have to do, is to change the line x=12
to x=8 , and the program will automatically
change all the lines with $x to show 8, instead
of 12. The other lines will be unaffected. Variables have
other important uses as well, as you will see later on.
CONTROL STRUCTURES
Control structures allow your program to make decisions
and to make them more compact. More importantly as well,
it allows us to check for errors. So far, all we have done
is write programs that start from the top, and go all the
way to the bottom until there are no more commands left
in the program to run. For instance:
#!/bin/bash
cp /etc/foo .
echo "Done."
This little shell program, call it bar.sh ,
copies a file called /etc/foo into the current
directory and prints "Done" to the screen. This program
will work, under one condition. You must have a file
called /etc/foo . Otherwise here is what happens:
xconsole$ ./bar.sh
cp: /etc/foo: No such file or directory
Done.
So you can see, there is a problem. Not everyone who runs
your program will have /etc/foo in their system.
It would perhaps be better if your program checked if /etc/foo
existed, and then if it did, it would proceed with the copying,
otherwise, it would quit. In pseudo code, this is what it
would look like:
if /etc/code exists, then
copy /etc/code to the current directory
print "Done." to the screen.
otherwise,
print "This file does not exist." to the screen
exit
Can this be done in bash ? Of course! The
collection of bash control structures are,
if , while , until ,
for and case . Each structure is
paired, meaning it starts with a starting "tag" and ends
with an ending "tag". For instance, the if
structure starts with if , and ends with fi .
Control structures are not programs found in your system.
They are a built in feature of bash . Meaning
that from here on, you will be writing your own code, and
not just embedding programs into your shell program.
if ... else ... elif ... fi
One of the most common structures is the if
structure. This allows your program to make decisions, like,
"do this if this conditions exists, else, do something else".
To use the if structure effectively, we must
make use of the test command. test
checks for conditions, that is, existing files, permissions,
or similarities and differences. Here is a rewrite on bar.sh :
#!/bin/bash
if test -f /etc/foo
then
# file exists, so copy and print a message.
cp /etc/foo .
echo "Done."
else
# file does NOT exist, so we print a message
and exit.
echo "This file does not exist."
exit
fi
Notice how we indent lines after then and
else . Indenting is optional, but it makes reading
the code much easier in a sense that we know which lines
are executed under which condition. Now run the program.
If you have /etc/foo , then it will copy the
file, otherwise, it will print an error message. test
checks to see if the file /etc/foo exists.
The -f checks to see if the argument is a regular
file. Here is a list of test 's options:
-d check if the file is a directory
-e check if the file exists
-f check if the file is a regular file
-g check if the file has SGID permissions
-r check if the file is readable
-s check if the file's size is not 0
-u check if the file has SUID permissions
-w check if the file is writeable
-x check if the file is executable
else is used when you want your program to
do something else if the first condition is not met. There
is also the elif which can be used in place
of another if within the if . Basically
elif stands for "else if". You use it when
the first condition is not met, and you want to test another
condition.
[-next
page-] [-previous page-]
|