morse script 1
Last night I got it to the point where it'd take single characters of user input and play them as a long beep if 1, a short beep if 0 and error if other.
It would only take one digit at a time though; a string of digits was rejected.
At that stage it looked like this:
#!/bin/bash
echo "Enter a sequence of 1s and 0s representing dashes and dots:"
read user_input
for x in $user_input; do
if [[ $x = 1 ]]
then beep -l 300 -D 50
elif [[ $x = 0 ]]
then beep -l 150 -D 50
else echo "$x is not an acceptable character"
fi
done
I recognise that this is poorly formed. Anyway, I need it to iterate over each character in the user input. I see references to sed and awk and whatnot, and I don't understand that stuff, so I'm going to use the most basic form I can. I found the answer here:
You can use a traditional for loop:foo=string
for (( i=0; i<${#foo}; i++ )); do
echo ${foo:$i:1}
done
${#foo}
expands to the length offoo
.${foo:$i:1}
expands to the substring starting at position$i
of length 1.
So now I have the following, which seems to be working OK:
#!/bin/bash
echo "Enter a sequence of 1s and 0s representing dashes and dots:"
read user_input
# iterate over each character in the string
for (( i=0; i<${#user_input}; i++ )); do
# beep long, short or reject input
# for x in $user_input; do
if [[ ${user_input:$i:1} = 1 ]]
then beep -l 300 -D 50
elif [[ ${user_input:$i:1} = 0 ]]
then beep -l 150 -D 50
else echo "${user_input:$1:1} is not an acceptable character"
fi
done
It was only in the second verison that I actually extended the beep lengths and added a delay after each one. originally the commands were beep -l 100 and beep -l 50 respectively. extending these to 300 and 150 with a -D delay of 50 after each repetition (of which there is only one per beep command, so it only added 50 after each beep).
Next steps:
* get it to read from the morse text file using a similar method?
* reverse the order so it's asking for input instead of taking it
* put something in to account for upper and lower case letters
* put in speed variables?
* define a function for beep to optimise code?
* design custom beep commands for each letter?
* how can I measure the efficiency of a script to decide?
Also:
* write in python?
* write in C++?
Comments
Post a Comment