[Ilugc] some shell script samples

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Wed, 26 Dec 2012 09:38:18 +0530

You do simple programming constructs using ksh like this:

$ cat sample.sh
#!/bin/ksh

echo -n "Please enter a number:"
read n
echo "You entered $n"

echo "Do you want to square it? Y/N"
read res
if [ $res = "Y" ]; then
    square=`expr $n \* $n`
     echo "Square of $n is $square"
else
       echo "Bye"
fi

You can move on to really feature rich scripts.

Basic looping is provided by the pleasant for() construct which is
 very different from the C for.

It is also found in perl.

Let us see a sample.

$ looping

for f in `ls /etc/*`
do
            echo "$f"
             type=`file $f`
            echo "$f is of type $t"
done

Please note that I use backticks '`" which is the same key as ~.

~ is called as tilde and ` is known as backquote or backtick.

You use the latter for evaluating shell expressions.

t='date'
t="date"
are both useless. ;)

t=`date`

is useful...

-Girish
-- 
Gayatri Hitech
http://gayatri-hitech.com

Other related posts: