[Ilugc] [TIP] shifting gears to shell scripting

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Mon, 19 Dec 2011 20:45:11 +0530

Shell scripting is as related to perl programming as a knife is
related to a sword.

But that is not to say that perl is always superior to shell scripting.

Many things that shell scripts can do is unnatural to do with perl.

As is the common case the language proficiency determines the choice
rather than the appropriateness
 of the situation in question.

A simple shell script is written like this:

#!/bin/sh

print "Hello World";

Not like this:

#!/bin/bash

echo "Hello World";

Bash is not universal. It is also not a good shell to learn shell
scripting. And please avoid
 tcsh or csh.

ksh is what you have to use.

Anyway we will focus on shell scripting and leave the argument to
those who like to waste time.

Now, a really nice effect I like to show to students learning shell
scripts is this:

$ cat i.sh
#!/bin/sh

for i in `jot 10 1 10`
do
        echo "Your tea is getting ready!"
        sleep 1
done

This won't work in Linux. You have to replace jot with seq and tweak it.

for loops in shell are an iterator construct. Just like in perl.

Only while gives you looping in shell.

An iterator is just a way to go over a list of values one by one.

For instance,

for foo in 1 3 3l4 something "nothing else"
do
         echo $foo
done

will go over each value one by one.

And in shell scripts each value is delimited by a space or a newline.

This is how UNIX works and UNIX utilities work.

No big wonder here.

We sometimes don't like it. We don't want to lose newlines. I will tell you
 what to do about that later.

For now, shell scripting requires that you know the various UNIX utilities like:

sleep
cut
grep
find
ls
date
su
tail
head

Take your time to learn them.

-Girish

-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts:

  • » [Ilugc] [TIP] shifting gears to shell scripting - Girish Venkatachalam