Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2013
    Posts
    1

    Unanswered: what does the below script do?

    Hi all,

    am completely new to shell scripting and assigned a task where the script needs to be modifies to achieve some task,so priory am trying to get what the existing script is performing,Please let me understand the highlighted portions

    #!/bin/ksh

    SOURCE_DIR="some path"
    SOURCE_MAPPING_FILE="some path"
    ERROR_FILE="some path"
    DESTINATION="some path"
    ARCHIVE_DIR="some path"
    COUNT_LOG="some path"

    echo "Begin unzip at `date`"

    for i in $SOURCE_DIR/*
    do

    file_name=`basename $i`
    file_lookup_name=`echo $file_name | cut -d'_' -f1`[/COLOR]
    email_address=`grep -i /$file_lookup_name/ $SOURCE_MAPPING_FILE | grep -v "^#" | cut -d" " -f 3`
    echo $email_address
    file_name2=filenametmp
    file_name3=tail
    file_name4=head
    file_name5=count
    filename_len=`basename $i | wc -m`
    echo "The filename is $file_name and its length is $filename_len"
    if [ "$filename_len" -gt 2 ]
    then
    mapping=`grep -i /$file_lookup_name/ $SOURCE_MAPPING_FILE | grep -v "^#" | cut -d" " -f 2`
    echo $mapping
    mapping_length=`echo $mapping | wc -m`
    echo $mapping_length
    echo "Processing File $i"

    if [ "$mapping_length" -gt 1 ]
    then
    # Test whether zip file is GOOD or BAD
    unzip -t $i > /tmp/$file_name.txt 2>&1
    echo /tmp/$file_name.txt
    zip_file_check=`tail -1 /tmp/$file_name.txt | cut -c 1-18`
    echo $zip_file_check
    if [ "$zip_file_check" = "No errors detected" ];
    then
    echo "File is Good"
    destination_dir=$DESTINATION$mapping
    echo $destination_dir
    echo $mapping


    unzip -l $SOURCE_DIR/$file_name>/tmp/$file_name2.txt
    line_count=`wc -l </tmp/$file_name2.txt`
    line_count2=` expr $line_count - 5`
    echo $line_count2
    tmp_dir=/tmp/$file_name2.txt
    sed -n '4,$p' $tmp_dir | head -$line_count2 | grep -v ".*\[HS-[0-9]*].*">/tmp/$file_name3.txt
    count=`wc -l </tmp/$file_name3.txt`
    echo $count
    mai=" File $file_name has $count reports"
    echo $mai | mailx -s " $file_name Stats " -c "x@x.com" $email_address
    echo "Mail Sent"
    echo "FILE $file_name is good and has been processed and has $count reports and mail has been sent to $email_address at `date`" >> $COUNT_LOG
    unzip $i -d $destination_dir
    chmod -R 664 $destination_dir/*
    else
    echo "FILE $file_name has not been processed as it failed in unzip" | mailx -s "Relay Server File Processing Error" x@x.com -- -f do-not-reply@x.com
    echo "FILE $file_name has not been processed as it failed in unzip at `date`" >> $ERROR_FILE
    echo "FILE $file_name is not good and has not been processed as it failed in unzip and mail has been sent to $email_address at `date`" >> $COUNT_LOG
    fi
    rm -f /tmp/$file_name.txt
    rm -f /tmp/$file_name2.txt
    rm -f /tmp/$file_name3.txt
    mv $i $ARCHIVE_DIR/$file_name
    else
    echo "No Mapping present for $i" | mailx -s "Relay Server File Processing in BOXI PROD" x@x.com
    echo "No Mapping present for $i"
    fi
    else
    echo "No files to Process, sleeping"
    fi
    done

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    What highlighted portion(s)?
    This part?:
    Code:
    . . . 
    do
    
    file_name=`basename $i` (1)
    file_lookup_name=`echo $file_name | cut -d'_' -f1` (2)
    . . .
    (1) "basename" removes the path from the full file name "$i"
    (2) "cut" extracts the first portion (field) of the file name split by the '_' separator
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •