Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:15:07+00:00 2026-06-11T08:15:07+00:00

A few days ago, I had a problem with a script dealing with a

  • 0

A few days ago, I had a problem with a script dealing with a multitude of files on our EMC mass filers. Here’s now my working code

unset xP_Array
declare -a xP_Array
...
export LOG=$HOME/BIN/somelogfile
export OUT=/path/to/device
...
echo "`date '+%m/%d/%y %T:'` START -- MEM"                          >> $LOG

echo "`date '+%m/%d/%y %T:'` Go to work directory."                                              >> $LOG
cd ${OUT}

echo "`date '+%m/%d/%y %T:'` Fill the array."                                                   >> $LOG
for f in "$OUT"/*XML; do
    xP_Array+=( "${f#$OUT/}" )
done
echo "`date '+%m/%d/%y %T:'` Get array length."                                               >> $LOG
Plen=${#xP_Array[@]}

echo "`date '+%m/%d/%y %T:'` MEM: $Plen FILES TO PROCESS."                                      >> $LOG

echo "`date '+%m/%d/%y %T:'` Check if zero files."                                               >> $LOG
date_fmt='%m/%d/%y %T'
if (( Plen = 0 ))
then
    printf "%($date_fmt)T: ZERO FILES\n" $(date +%s)                                            >> $LOG
fi

echo "`date '+%m/%d/%y %T:'` Loop."                                                             >> $LOG
for i in "${xP_Array[@]}"
do
        echo "`date '+%m/%d/%y %T:'` Move file to run directory."                              >> $LOG
        mv $OUT/$i RUN/
        echo "`date '+%m/%d/%y %T:'` PROCESSING "$i"."                                          >> $LOG
        [[[DATABASE LOAD DONE HERE]]]
        echo "`date '+%m/%d/%y %T:'` Check DB LOAD return value."                                       >> $LOG
        EXIT=`echo $?`
        case $EXIT in
                0) echo "`date '+%m/%d/%y %T:'` COMPLETE."                                      >> $LOG
                mv RUN/"$i" "$ARCH"
                ;;
                *) echo "`date '+%m/%d/%y %T:'` ERROR. "$i" MOVED TO RECON."                    >> $LOG
                mv RUN/"$i" "$RECON"
                ;;
        esac
done

echo "`date '+%m/%d/%y %T:'` END -- MEM"                                    >> $LOG

I wonder if it could work faster. I am already working with my DBA to see if the database inserts can be sped up but I wonder if the loop itself could run faster.

Btw, all the echo statements are redirected to a log file that I email myself when the script completes. Are they slowing the script down?

Can this script be optimized to run faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:15:09+00:00Added an answer on June 11, 2026 at 8:15 am
    for f in "$OUT"/*XML; do
        xP_Array+=( "${f#$OUT/}" )
    done
    

    could be replaced with

    pushd "$OUT"
    xP_Array=( *XML )
    popd
    

    but I don’t think that’s a big bottleneck in your script.

    In your other loop, the only real overhead I see is the repeated calls to date and
    using command substitution to assign $? to EXIT, which can be done directly (EXIT=$?). I don’t think there is anything else to optimize there except the actual DB load.

    If you were willing to switch from human readable dates, you could assign the current time (as a UNIX timestamp) to SECONDS, then just reference that variable for the log lines instead of calling date.

    SECONDS=$(date +%s)
    for i in "${xP_Array[@]}"
    do
        echo "$SECONDS: Move file to run directory." >> $LOG
        # etc
    done
    

    With a new-enough bash (4.2 or later, I think), printf can format the UNIX timestamp as a readable time:

    printf "%(%m/%d/%Y %T)T" $SECONDS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a question I had stumbled upon a few days ago. The question is
It was working few days ago, don't know what had went wrong... **undefined method
I had an iPad stolen a few months ago. A few days ago, the
Few days ago I had to reinstall all my Linux system, and I also
A few days ago, I had an issue with ASP.Net threading. I wanted to
I am using jQuery-1.4.2 with CakePHP and a few days ago I had this
I had downloaded a Xcode 4.2.1 for lion a few days ago. But strangely,
I posted a thread on here a few days ago relating to this file,
I had developed a font few days ago. And i wanted to make it
I am started using MongoDB few days ago, and i have problem in understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.