Optimizing script to reduce execution time

AFILENAME=glow.sh
                FILENAME="/${AFILENAME}"
                WIDTHA=$(echo ${FILENAME} | wc -c)
                NTIME=0
                RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1)
                do
                        WIDTHTIMES=$(awk "BEGIN{printf \"%0.f\n\", $WIDTHA * $NTIME}")
                        EMPTY="$(printf 'u1u1.%*s' $WIDTHTIMES)"
                        if [ $NTIME -le $WIDTHA ] ; then
                                echo "$EMPTY"
                                echo "${EMPTY}${eachletter}${EMPTY}"
                                echo "$EMPTY"
                        fi
                        NTIME=$(($NTIME + 1))
                done)
                Agency=$(echo "${RESULTS}" | tr -d '\n')

the above works well for what i need it to do.

However, as many of you more experienced users can see, it is somewhat inefficient. i was wondering if there's a portable way to rewrite this.

the script basically creates a unique identifier based off the name of a file/script.

Everything you can do in bash (faster) that is done in a child process is in red:

                FILENAME="/${AFILENAME}"
                WIDTHA=$(echo ${FILENAME} | wc -c)  use mapfile instead
                NTIME=0
                RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1)
                do
                        WIDTHTIMES=$(awk "BEGIN{printf \"%0.f\n\", $WIDTHA * $NTIME}")
                        EMPTY="$(printf 'u1u1.%*s' $WIDTHTIMES)"
                        if [ $NTIME -le $WIDTHA ] ; then
                                echo "$EMPTY"
                                echo "${EMPTY}${eachletter}${EMPTY}"
                                echo "$EMPTY"
                        fi
                        NTIME=$(($NTIME + 1))
                done)
                Agency=$(echo "${RESULTS}" | tr -d '\n') 

Okay. It also looks to me like the entire RESULTS=( ) loop construct should be done with one awk invocation.

1 Like

Wouldn't it be nice if people knew what the desired output was? I can see a long, unstructured (?) sequence of "u1u1"s and long runs of spaces, interspersed with single characters from the original filename. jim mcnamara is certainly right when suspecting all this can be done with one awk , but who knows HOW that would / should be achieved?

And, the resulting "identifier" is 1199 char long - may work in some proprietary systems, but will certainly not be manageable for e.g. file names' in all *nix file systems.
Did you notice that $WIDTHA does NOT represent the file name's length but is one larger?

1 Like

Hi.

Portable among what hardware / software systems, what languages, what etc.? ... cheers, drl

1 Like

This gives an identical output - for any purpose whatsoever - in one awk statement:

awk -vFN="$FILENAME" '
BEGIN   {LFN = length(FN) + 1
         for (i=0; i<LFN-1; i++){EMPTY = sprintf ("u1u1.%*s", i*LFN, "")
                                 printf "%s\n%s%s%s\n%s\n", EMPTY, EMPTY, substr(FN, i+1, 1), EMPTY, EMPTY
                                }
        }
'
2 Likes

this is good enough for me. thank you very much!!!!
it is perfect!