**********************************************************************
Example 1: An experiment generates a set of data files for each run:
data1, data2,... Rerun the experiments generates a new set of data 
files: data1.1, data2.1,... Compare data files to see which data
is different.

#!/bin/bash

for file in *
do
 if [ -e $file.1 ]
  then
   diff $file $file.1 &> /dev/null  
    # /dev/null buries the output of the "diff" command.
    #   cmp -s $1 $2  has same result ("-s" silent flag to "cmp")
    #   Thank you  Anders Gustavsson for pointing this out.
    #
    # Also works with 'diff', i.e.,   diff $1 $2 &> /dev/null

   if [ $? -eq 0 ]         # Test exit status of "cmp" command.
    then
     echo "File \"$file\" is identical to file \"$file.1\"."
    else  
     echo "File \"$file\" differs from file \"$file.1\"."
   fi
 fi
done

exit 0

***********************************************************************
Example 2: I have a list of words in a file words.data. To see which 
word is valid by looking a dictionary.

#!/bin/bash
# lookup: Does a dictionary lookup on each word in a data file.
                                                                                
file=words.data  # Data file from which to read words to test.
                                                                                
echo
                                                                                
while [ "$word" != end ]  # Last word in data file.
do
  read word      # From data file, because of redirection at end of loop.
  look $word > /dev/null  # Don't want to display lines in dictionary file.
  lookup=$?      # Exit status of 'look' command.
                                                                                
  if [ "$lookup" -eq 0 ]
  then
    echo "\"$word\" is valid."
  else
    echo "\"$word\" is invalid."
  fi
                                                                                
done <"$file"    # Redirects stdin to $file, so "reads" come from there.
                                                                                
echo
                               
exit 0

************************************************************************
Example 3: To check which file in current directory is script.

#!/bin/bash
# script-detector.sh: Detects scripts within a directory.

TESTCHARS=2    # Test first 2 characters.
SHABANG='#!'   # Scripts begin with a "sha-bang."

for file in *  # Traverse all the files in current directory.
do
  if [[ `head -c$TESTCHARS "$file"` = "$SHABANG" ]]
  #      head -c2                      #!
  #  The '-c' option to "head" outputs a specified
  #+ number of characters, rather than lines (the default).
  then
    echo "File \"$file\" is a script."
  else
    echo "File \"$file\" is *not* a script."
  fi
done
  
exit 0

What takes place between [[ and ]]? there is parameter expansion and command substitution.

*************************************************************************
Factorial now returns a value

factorial()
{
  if [ "$1" -gt "1" ]; then
    i=`expr $1 - 1`
    factorial $i
    return `expr $1 \* $?`
  else
    return 1
  fi
}

## Main script

echo Please enter a number
read num
factorial $num
echo Factorial of $num is $?

the return value is tied to exit status which is limited in 0-255.
How to get away from it?

*************************************************************************

Classical recursive example: factorial

factorial()
{
  if [ "$1" -gt "1" ]; then
    i=`expr $1 - 1`
    j=`factorial $i`
    k=`expr $1 \* $j`
    echo $k
  else
    echo 1
  fi
}

## Main script

echo Please enter a number
read num
factorial $num

echo the return value to stout, then assign the output of the function
to a variable using command substitution.

***********************************************************************

######################################################
A cut example: list users from /etc/passwd
#####################################################

#! /bin/sh
                                                                                
# List all the users in /etc/passwd.
                                                                                
FILENAME=/etc/passwd
                                                                                
for user in $(cut -d: -f1 $FILENAME)
do
  echo $user
done


