Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a query regarding bash. I have been running some of my own C++ programs in conjunction with commercial programs and controlling their interaction (via input and output files) through Bash scripting. I am finding that if I run my c++ program alone in terminal it completes in around 10–15 seconds, but when I run the same through the bash script it can take up to 5 minutes to complete in each case.

I find using System Monitor that consistently 100% of one CPU is used when I run the program directly in terminal whereas when I run it in bash (in a loop) a maximum of 60% of CPU usage is recorded and seems to be linked to the longer completion time (although the average CPU usage is higher over the 4 processors).

This is quite frustrating as until recently this was not a problem.

An example of the code:

#!/usr/bin/bash
DIR="$1"
TRCKDIR=$DIR/TRCKRSLTS
STRUCTDIR=$DIR
SHRTTRCKDIR=$TRCKDIR/SHRT_TCK_FILES
VTAL=VTAL.png
VTAR=VTAR.png

NAL=$(find $STRUCTDIR | grep NAL)
NAR=$(find $STRUCTDIR | grep NAR)
AMYL=$(find $STRUCTDIR | grep AMYL)
AMYR=$(find $STRUCTDIR | grep AMYR)
TCKFLS=($(find $TRCKDIR -maxdepth 1 | grep .fls))

numTCKFLS=${#TCKFLS[@]}

for i in $(seq 0 $[numTCKFLS-1]); do

        filenme=${TCKFLS[i]}
        filenme=${filenme%.t*}
        filenme=${filenme##*/}      

        if [[ "$filenme" == *VTAL* ||  "$filenme" == *VTA_L* ]]; then

            STREAMLINE_CUTTER -MRT ${TCKFLS[i]} -ROI1 $VTAL -ROI2 $NAL  -op "$SHRTTRCKDIR"/"$filenme"_VTAL_NAL.fls
            STREAMLINE_CUTTER -MRT ${TCKFLS[i]} -ROI1 $VTAL -ROI2 $AMYL  -op "$SHRTTRCKDIR"/"$filenme"_VTAL_AMYL.fls

        fi

        if [[ "$filenme" == *VTAR* ||  "$filenme" == *VTA_R* ]];then

            STREAMLINE_CUTTER -MRT ${TCKFLS[i]} -ROI1 $VTAR -ROI2 $NAR  -op "$SHRTTRCKDIR"/"$filenme"_VTAR_NAR.fls
            STREAMLINE_CUTTER -MRT ${TCKFLS[i]} -ROI1 $VTAR -ROI2 $AMYR  -op "$SHRTTRCKDIR"/"$filenme"_VTAR_AMYR.fls

        fi

done
share|improve this question
    
How much of the execution time is being spent in those find calls? And why don't you use, for example, NAL=$(find $STRUCTDIR -name "*NAL*") (or something even more precise) instead of listing the entire directory tree under DIR and then grepping out the single matching file? (Also, without any utility function at all: TCKFLS=($TRCKDIR/*.fls)) –  rici Aug 20 '13 at 15:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.