Function explanation


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Function explanation
# 1  
Function explanation

dear sir,

I am new to unix zone. need some explanation on the function used.

Code:
 cat /apps/prd/venue/code/bin/std.funcs
#!/usr/bin/ksh
#-------------------------------------------------------------------
# printmsg: prints the message given in arg 1 with timestamp to
#           stdout or APPENDS to a file
#           if  printing to a file, arg 2 should be the file name
#           and arg 3 is the number of tabs, otherwise if printing to
#           stdout, then arg 2 will be the number of tabs.
#                               if numtabs is omitted, default is 0
#
#                               NOTE: filename must contain at least one character
#                               or printmsg will assume that it is tab argument
#
#                               if numTabs=0 then entire date/time is printed
#                               if numTabs>0 then only time is printed
#
# input:    message=$1  appendfile=$2  numtabs=$3
#                               message=$1  numtabs=$2
#           message=$1  appendfile=$2
#

#-------------------------------------------------------------------
function printmsg
{
        set +x
        set +u  #allow unset variables so input parameters can be omitted

        #declare local variables
        typeset -i numTabs maxLen prefixLen currLen
        typeset -t msg argTWO argTHREE fileName
        typeset -t margin gap dt
        typeset -t inLine chunk nextWord firstLine

        # Constant Variables
        maxLen=78
        gap='    '  #set to four spaces

        # Input variables & initilizations
        msg=$1
        argTWO=$2
        argTHREE=$3
        fileName=''
        margin=''
        margin2=''
        firstLine=TRUE
        chunk=""

        if [[ $(echo $argTWO | tr -d '[-0-9]') = ""  ]] then
                numTabs=${argTWO:=0}
        else
                numTabs=${argTHREE:=0}
                fileName=$argTWO
        fi

        if [[ $numTabs -le 0 ]] then
                dt=$(date)
        else
                dt=$(date +%H:%M:%S)
        fi

        while [[ $numTabs -gt 0 ]];
        do
                margin=${margin}${gap}
                numTabs=$numTabs-1
        done

        ## Build margin for extra line(s) that is as wide as the prefix.
        ## Extra lines will not have date on them, they will be blank.
        prefix="${margin}${dt}${gap}"
        prefixLen=${#prefix}
        while [[ $prefixLen -gt 0 ]];
        do
                margin2=${margin2}' '
                prefixLen=$prefixLen-1
        done

        inLine=${msg}

        ## Loop through each word in $inLine.  Print lines that will fit
        ## within the $maxLen limit.
        for word in $inLine
        do
                currLen=$((${#prefix}+${#chunk}+1+${#word}))

                if [[ -z $chunk ]] then
                        chunk=$word
                elif [[ $currLen -lt $maxLen ]] then
                        chunk=$chunk' '$word
                else
                        if [[ -z $fileName ]] then
                                print "${prefix}${chunk}"
                        else
                                print "${prefix}${chunk}" >> $fileName
                        fi

                        chunk=$word

                        if [[ $firstLine = "TRUE" ]] then
                                firstLine=FALSE
                                prefix=$margin2
                        fi
                fi
        done

        # Print the last line.
        if [[ -z $fileName ]] then
                print "${prefix}${chunk}"
        else
                print "${prefix}${chunk}" >> $fileName
        fi

        return 0
} # end printmsg

#------------------------------------------------------------------------------
# Function:  trim_file
# Description:
#
#   This function shortens a specified file to a maximum number of lines.
#   It will remove lines at the beginning of the file to satisfy the max
#   line constraint.
#
#   If the file already has less than the maximum number of lines, it will
#   be unchanged.
#
# Input:
#   FILE ($1) - The file to shorten.
#   MAX_LENGTH ($2) - The length to shorten FILE.
#
# Output:
#   FILE will be shortened to contain a maximum of MAX_LENGTH number of lines.
#
#   Return Codes:
#      SUCCESS - is returned if the number of lines in the FILE is greater than
#         MAX_LENGTH and the FILE is shortened to contain the last MAX_LENGTH
#         lines.
#      SUCCESS - is also returned if the number of lines in the FILE is less
#         than or equal to MAX_LENGTH.  In this case the FILE will be
#         unchanged.
#      FAIL - signifies an error with function.  Status messages are
#         printed to standard error.
#
#------------------------------------------------------------------------------
function trim_file
{
   typeset FUNCTION=$0
   typeset FILE=$1
   typeset MAX_LENGTH=$2
   typeset SUCCESS=0
   typeset FAIL=1

   # Local Variables
   typeset TEMP_FILE

   # Initalize TEMP_FILE.  TEMP_FILE will be used to hold the filename for
   # a file that will hold a temporary shortened version of FILE.
   TEMP_FILE=$(mktemp -d/tmp -p$FUNCTION)
   RC=$?
   if [[ $RC -ne $SUCCESS ]] then
      print -u2 "mktemp -d/tmp -ptrim_file, failed.  RC=$RC"
      return $FAIL
   fi

   # This command will print the last MAX_LENGTH lines in FILE to TEMP_FILE.
   tail -n $MAX_LENGTH $FILE > $TEMP_FILE
   RC=$?
   if [[ $RC -ne $SUCCESS ]] then
      print -u2 "tail -n $MAX_LENGTH $FILE > $TEMP_FILE, failed.  RC=$RC"
      return $FAIL
   fi

   # Replace FILE with its shortened version TEMP_FILE.
   cp $TEMP_FILE $FILE
   RC=$?
   if [[ $RC -ne $SUCCESS ]] then
      print -u2 "cp $TEMP_FILE $FILE, failed.  RC=$RC"
      return $FAIL
   fi

   rm -f $TEMP_FILE

   return $SUCCESS

}  ## end trim_file


#-----------------------------------------------------------------------------
# function: error_check
# This function is called to determine if the previous process exited with a
# nonzero return value.
# If it did, then send  mail to mailgroup with
# appropriate message $2 and subject $3
#
# Note: LOGFILE and MAILGROUP will be exported from the calling script
#-----------------------------------------------------------------------------

function error_check
{
# Set INDENT variable used with printmsg to write in the $LOGFILE
INDENT=${INDENT:-0}  ## if INDENT unset, then default to 0
let INDENT=INDENT+1
   if [[ $1 -ne 0 ]] then
      printmsg "$2 RC=$1" $LOGFILE $INDENT
      echo "$2 RC=$1" | mailx -s "$3" $MAILGROUP
      exit $1
   fi
   return
}

# 2  
Seems you already have nice descriptions of your functions, from the comments in your code.

What exactly are you looking for. Please be specific.

Thanks
This User Gave Thanks to Neo For This Post:
# 3  
i am not able to understand the trim_file function.
# 4  
This function is quite well commented, it basically uses the tail command to print the last MAX_LENGTH ($2) lines of FILE ($1) these are stored in a temporary file (name generated by the mktmp command) the contents of this temporary file is then copied back over the original file and the temporary file removed.
All the operations performed are checked to ensure they have a success exit code before moving onto the next step.

Which particular part of the function you are having trouble understanding?
This User Gave Thanks to Chubler_XL For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #179
Difficulty: Easy
The Apple I first went on sale in July 1976 and sold for $666 USD.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Shell Programming and Scripting

Need explanation

Hi, I need more explination on it, how it works abcd="$(echo "$abcd" | sed 's/ //g')" >> ${LOGFILE} 2>&1 can any one suggest me on this? Rgds, LKR (1 Reply)
Discussion started by: lakshmanraok
1 Replies

4. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

5. Shell Programming and Scripting

explanation of test function

I have found a code some where, which looks like if (test $value) then <do something> fi I am not understanding what is test doing here. I have seen test with !,-eq, -e etc. But, the above appears to be a new one to me. Can anyone please expalin me. (4 Replies)
Discussion started by: mady135
4 Replies

6. UNIX for Dummies Questions & Answers

In need of explanation

Its great someone provided this script that strips out a filename and extension but can someone explain how each line works? file1='Jane Mid Doe.txt' newfile='Jane.txt' 1) ext=${file1##*.} 2) filename=${file%%.???} 3) set -- $filename 4) newfile="1.$extension" (1 Reply)
Discussion started by: Lillyt
1 Replies

7. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

8. UNIX and Linux Applications

need explanation

Hi am having a c pgm. It has the include files (unistd.h,sys/types.h,win.h,scr.h,curses.h,stdarg.h and color.h). I don't know the purpose of these include files. will u plz explain me. (1 Reply)
Discussion started by: Mari.kb
1 Replies

9. Shell Programming and Scripting

tr explanation please

how does below tr command replace nonletters with newlines? I think I understand tr -cs '\n' part.. but what is A-Za-z\' <--- what is this?? tr -cs A-Za-z\' '\n' | -c --complement -s, --squeeze-repeats replace each input sequence of a repeated character that is... (1 Reply)
Discussion started by: convenientstore
1 Replies

10. Shell Programming and Scripting

tr explanation please

how does below tr command replace nonletters with newlines? I think I understand tr -cs '\n' part.. but what is A-Za-z\' <--- what is this?? tr -cs A-Za-z\' '\n' | -c --complement -s, --squeeze-repeats replace each input sequence of a repeated character that is... (0 Replies)
Discussion started by: convenientstore
0 Replies

Featured Tech Videos