Thursday 17 November 2011

Sample Shell Script 1: Sum all the argument passed to the script

#!/bin/sh
# ./argsum.sh abcd 45.1 89.2 12.3 efgh
############################################################################
# Name: argsum.sh
# Desc: Sum all the argument passed to the script. It accepts all type of
# arguments but make a SUM of only number type arguments
# Change Log
# Ver   Date            Author          Comment
# ---   -----------     -------------   ----------------------------------------
# 1.0   MM/DD/YYYY      SD              Creation
#
#############################################################################


LOGDIR=/home/nzuser/sankar
LOGFILE="${LOGDIR}/argsum.log"
ERRFILE="${LOGDIR}/argsum.err"
SUM=0


# Function to write log
log_msg()
{
MESSAGE=${@}
echo ${MESSAGE}>>${LOGFILE}
}


# Check if the log file already exists in the directory, remove the file
if test -f ${LOGFILE}
then
    rm -f ${LOGFILE}
fi


log_msg "Log directory: ${LOGDIR}"
log_msg "Log file: ${LOGFILE}"
log_msg "Error file: ${ERRFILE}"


if [ ${#} -gt 0 ]    # ${#} --> Number of Arguments
then
    log_msg "Number of arg: ${#}"
fi


for I in ${@}       # ${@} --> All arguement as space separated
do
    SUM=`echo ${SUM}+${I}|bc` 

    # Input values are accepted as character, converted to number
done

log_msg "Total of ${#} arg value is : ${SUM}"
echo "Get the run log from ${LOGFILE}"

No comments:

Post a Comment