###########
# C-shell #
###########
# This section might apply to ksh or bash as well, just try it


#
# The number of  commandline arguments  is given by $#
#
if [ $# -lt 3 ]; then
echo "Usage: bla [bla] [bla]"
fi


#
# Build a menu
#
echo Enter variable 1:
read var1
echo Enter variable 2:
read var2
echo You entered: $var1
echo ........and: $var2


#
# Get arguments from command line
#
for arg in $*; do
  echo $arg
done


#
# Read from a list of values and pipe output to a loop
#
cat amplitudes |
while read AMPLITUDE; do
  <command sequence>
done

find * -name <filename> -print |
while read foundfile; do
  <something with foundfile>
done


#
# This can be used to test on sufficient number of arguments
#
if [ $# -lt 1 ]; then
  echo "Please specify argument(s)"
  exit
fi


#
# Test for conditions
#
if [ $1 = '-h' ]; then
  echo "Here comes your help"
else
  echo "You think you need no help \?"
fi


#
# Assign the output of a command to a SHELL VARIABLE
#
VALUE=`command`


#
# using functions in a script
#
script_function() {
LOCALVAR1=$1
LOCALVAR2=$2
  <command sequence>
}

GLOBALVAR1="a b c"
GLOBALVAR2=10
script_function $GLOBALVAR1 $GLOBALVAR2



#
# Simple expressions can be evaluated as follows:
#
RESULT=`expr $VAR1 * $VAR2`
# in which * can be replaced by various operators



#
# Filenames and paths
#
FILENAME=`basename $FILE`
DIR=`dirname $FILE`


#
# Do simple arithmetic using dc  (DescCalc)
#
echo "3 5 + f" | dc




#
# walk through a list of values
#
$LIST="1 2 3 6 5 4"
for counter in $LIST; do
  echo $counter
done

for file in `ls $1`; do
  <command sequence>
done



######################
# Korn /Bourne Shell #
######################

#
# Assigning a value to a variable
#
export var=value


#
# Simple two-counter driven loop
#
X=0
while [ $X -lt 1200 ]
do
  Y=0
  while  [ $Y -lt 800 ]
  do
    (( Y=Y+100 ))
    echo Y $Y
  done
  (( X=X+120 ))
  echo X $X
done


#
# Calculations inside shell
#
let x=x+10
   is equal to:
(( x=x+10 ))

   also useful:
(( x++ ))

or

x=`expr $x + 5`


#
# Find out if a directory contains any files
#
if [ -f dir/* ]
then
  blabla
fi


##############
# MORE LOOPS #
##############

#
# if you have the seq command
#  seq is part of the gnu shellutils
#
echo "for loop"
for i in `seq 1 5`;
do
  echo $i
done    

#
# if you don't have the seq command you might use this
#
echo
echo "while loop"
i=1
while [ $i -le 5 ]; do
  echo $i
  i=`expr $i + 1`
done

#
# Here's another for loop
#
echo
echo "another for loop"
for (( i=0 ; i <= 5; i++))
do
 echo loop counter $i
done

