morse script 2
I'll copy in the actual code below later, but I can't right now because I'm using ssh to a headless machine at work.
I gave up on reading the letters from a separate text file - seemed like too much overhead for what I wanted to do. Instead I've found out how to create an associative array within my bash script. This'll let me pass the user input variable as a key that will return the binary value.
SO, I have the following:
#!/bin/bash
# declare array and assign values to each letter
# make sure no spaced between array var, = and (
declare -A morseCode_arr=( [a]=01 [b]=1000 [c]=1010 [d]=100 [e]=0 [f]=0010 [g]=110 [h]=0000 [i]=00 [j]=0111 [k]=101 [l]=0100 [m]=11 [n]=10 [o]=111 [p]=0110 [q]=1101 [r]=010 [s]=000 [t]=1 [u]=001 [v]=0001 [w]=011 [x]=1001 [y]=1011 [z]=1100 )
echo 'Input string'
read user_input
#loop over the characters if the user input
for (( s=0; s<${#user_input}; s++ )); do
# retrieve the value from the array and assign to a new variable
binary_output=${morseCode_arr[${user_input:$s:1}]}
echo ${binary_output}
# loop over the characters in the binary string reprenting each letter
for (( i=0; i<${#binary_output}; i++ )); do
# long beep for dash
if [[ ${binary_output:$i:1} == 1 ]]
then beep -l 300 -D 50
# short beep for dot
elif [[ ${binary_output:$i:1} == 0 ]]
then beep -l 150 -D 50
# error for anything else (should not be possible if the array is right)
else echo "${binary_output:$i:1} is not an acceptable character"
fi
done
# pause at the end of each letter
sleep .225
done
This works pretty well. It takes your input, prints the binary string while making the beeps that represent each letter in turn.
next steps:
* get it to randomly pick letters of the alphabet and test the user
* get it to randonly pick words from the dictionary file and test the user
* levels (letters, words, sentences)
* speed
The following was helpful in this stage of proceedings:
http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/
I gave up on reading the letters from a separate text file - seemed like too much overhead for what I wanted to do. Instead I've found out how to create an associative array within my bash script. This'll let me pass the user input variable as a key that will return the binary value.
SO, I have the following:
#!/bin/bash
# declare array and assign values to each letter
# make sure no spaced between array var, = and (
declare -A morseCode_arr=( [a]=01 [b]=1000 [c]=1010 [d]=100 [e]=0 [f]=0010 [g]=110 [h]=0000 [i]=00 [j]=0111 [k]=101 [l]=0100 [m]=11 [n]=10 [o]=111 [p]=0110 [q]=1101 [r]=010 [s]=000 [t]=1 [u]=001 [v]=0001 [w]=011 [x]=1001 [y]=1011 [z]=1100 )
echo 'Input string'
read user_input
#loop over the characters if the user input
for (( s=0; s<${#user_input}; s++ )); do
# retrieve the value from the array and assign to a new variable
binary_output=${morseCode_arr[${user_input:$s:1}]}
echo ${binary_output}
# loop over the characters in the binary string reprenting each letter
for (( i=0; i<${#binary_output}; i++ )); do
# long beep for dash
if [[ ${binary_output:$i:1} == 1 ]]
then beep -l 300 -D 50
# short beep for dot
elif [[ ${binary_output:$i:1} == 0 ]]
then beep -l 150 -D 50
# error for anything else (should not be possible if the array is right)
else echo "${binary_output:$i:1} is not an acceptable character"
fi
done
# pause at the end of each letter
sleep .225
done
This works pretty well. It takes your input, prints the binary string while making the beeps that represent each letter in turn.
next steps:
* get it to randomly pick letters of the alphabet and test the user
* get it to randonly pick words from the dictionary file and test the user
* levels (letters, words, sentences)
* speed
The following was helpful in this stage of proceedings:
http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/
Comments
Post a Comment