A shell is a covering with nothing substantial inside like sea shells
from which
the word came.
In the UNIX world shells are meant to say something other than the kernel.
But in reality it is not just the shell and kernel. There are many
other entities.
The C library called glibc in the Linux duniya and the libc in UNIX,
the binutils
package which is part of the OS base system and several other pieces
go towards
making a complete system.
And then people say that Linux is just a kernel and that you need GNU
utilities and
packaging to make a Linux distro.
Linux is a brand and a commonly understood word to mean anything
related to UNIX.
Most people think UNIX is academic and old and that everything they
say today is one of
the various incarnations and cousins of Linux.
This is not correct and I will stop at that. Let us see shell scripting.
You have different ways to count in shell scripts. I used to use the
expr command. It is messy.
Then I learnt the let command. It is nice.
You are free to use other UNIX command line utilities too. Every shell
comes with what are
known as "shell built ins".
Typically ls,date,pwd and so on are shell builtins.
And every shell also has command line editing features like auto
completion, history, line
yanking, killing and so on.
Bash has made a lot of innovations in this area and it is a really huge shell.
ksh has many of the good features of bash without the bloat and the bugs.
You are free to stick to bash for learning and then when you graduate
you can start using ksh.
Now, let us look at a simple while loop with counting. As I said, for
is only an iterator. It is not a
real loop like while.
$ cat i.sh
let i=100
while [ $i -gt 0 ];
do
echo $i
let i--
done
This is a nice way to iterate with a while loop.
I repeat.
for takes a list of values beforehand and runs the loop thro' them
like list of files or
list of numbers or list of strings or a combination.
while has a condition like in C and runs the loop infinitely till the
condition is true.
The expression
[ $i -gt 0 ];
is nothing but the test command.
$ man test
Instead of [], we can also use test.
Depending on the true value, while runs.
And both for and while rely on the body being enclosed in
do
done
There are two more constructs with a body.
Obviously they are conditionals and not loops.
if [ $i -eq 0 ]; then
echo "i is equal to 0"
fi
The loop terminator is the keyword in reverse.
Similarly with switch statement.
case $i in
...
esac
We will see the case statement later.
It is the only shell thing that understands regular expressions.
Let us finish with one more way to do counting. As I said with expr.
$ cat i.sh
i=100
while [ $i -gt 0 ];
do
echo $i
i=`expr $i - 1`
done
If you leave out the spaces or include spaces where you shouldn't it won't work.
Execute it exactly the way you see it.
-Girish
--
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in