Broken: Elif in For loop with increment counter
I wrote a short script to generate a formatted string for synthesizing syllables in macintalk. I would like to insert a silence every 20 syllables, so I wrote a for loop with a counter stored in the temp file and a nested elif statement that should add a silence command every time the counter reaches 20. Then the counter is reset after it reaches 20 ...
For some reason, I can't swear, elif is only available the first time the condition is found true. In other words: the first time the counter reaches 20, it inserts a silence command, resets, and continues the loop. The second time the counter reaches 20, elif is denied access and continues to increase until $ k reaches its maximum value.
Does anyone know why this code won't work?
EDIT ** Here's a slightly longer snippet by popular request =)
Here are the links to the files if you want to try and reproduce: syllables_phncode.txt cbsyllindx.txt
# Load Syllables
echo ''
echo 'Opening Syllable Transcription for Victoria'
echo ''
syllfile=./syllables_phncode.txt
syllables=$(cat $syllfile)
syllarray=()
counter=0;
# Create Indexed Array of Phonological Code for Syllables
for k in $syllables
do
echo $counter
echo $k
syllarray[counter]=$k
counter=$(($counter + 1));
done
# Load syllable index for stimulus stream
indxfile=./cbsyllindx.txt
indx=$(cat $indxfile)
stream=''
tempfile=count.tmp
echo 0 > $tempfile
echo '====================================================================================='
echo 'Counter Balanced Stimulus Order (Indexed by Syllables in Alphabetical Order)'
echo '====================================================================================='
echo $indx
echo '====================================================================================='
echo ''
echo 'Creating counterbalanced stimulus stream string with proper Macintalk formatting'
echo ''
for k in $indx
do
counter=$[$(cat $tempfile) + 1]
echo $counter > $tempfile
if [ $k -eq 0 ]; then
stream=$stream'@_'${syllarray[k]}
elif [ $counter -eq 20 ]; then
echo Adding Silence after syllable: ${syllarray[k]}
stream=$stream'_'${syllarray[k]}'[[ slnc 20 ]]'
echo 0 > $tempfile
else
stream=$stream'_'${syllarray[k]}
fi
done
unlink $tempfile
echo '------------------------------------------------------'
echo 'Printing Stream to Screen'
echo '------------------------------------------------------'
echo $stream
Here is the line generated by this script:
_tUW_dAE_rOW_pIY_gOW_lAE @_bIY_kUW_tIY_tUW_dAE_rOW_pIY_gOW_lAE_bUW_dOW_pAE_pIY_gOW [[slnc20]] _ LAE @_bIY_kUW_t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gOW_lAE @_bIY_kUW_tIY_tUW_dAE_rOW_pIY_gOW_lAE_bUW_dOW_pAE
source to share
Your problem is that you hit the string $k -eq 0
straight when $counter
equals 20
, but you increment the counter anyway, and then after going through 20
, you can never hit it again.
This can be seen by adding set -x
to the beginning of your script and running it and looking at the output.
You don't want if / else for unrelated checks to have this problem.
You probably need something more like the body of your loop.
if [ $k -eq 0 ]; then
stream=${stream}@
fi
stream=${stream}_${syllarray[k]}
if [ $counter -eq 20 ]; then
stream=$stream'[[ slnc 20 ]]'
fi
Retains individual problems.
You also don't (as pointed out in the comments) at all here on the tempfile. (You don't have any sub-wrappers other than those related to reading your files, which don't count.)
You can also avoid liners $(cat file)
by using instead $(< file)
.
You should also use $((...))
instead $[...]
.
source to share