Score:3

failing to add an exit to this loop

ag flag

Found this interesting script that draws snow inside your terminal, however.... I would like to have an option to type 'q' at any time to exit the loop. This is what I have;

 

 LINES=$(tput lines)
COLUMNS=$(tput cols)
 
declare -A snowflakes
declare -A lastflakes
 
clear

function move_flake() {    

i="$1"
 
if [ "${snowflakes[$i]}" = "" ] || [ "${snowflakes[$i]}" = "$LINES" ]; then
snowflakes[$i]=0
else
if [ "${lastflakes[$i]}" != "" ]; then
printf "\033[%s;%sH \033[1;1H " ${lastflakes[$i]} $i
fi
fi
 
printf "\033[%s;%sH*\033[1;1H" ${snowflakes[$i]} $i
 
lastflakes[$i]=${snowflakes[$i]}
snowflakes[$i]=$((${snowflakes[$i]}+1))
}
 
while :
do
    
i=$(($RANDOM % $COLUMNS))
 
move_flake $i

for x in "${!lastflakes[@]}"
do
move_flake "$x"
done
 
sleep 0.1
done

this is that I have tried to add ;

echo "Type 'q' to exit out"
while read -n1 -r -p "want to exit the script?"
do
    if [[ $REPLY == q ]];
    then
        break;
    else
        #whatever
    fi
done

added it inside the function, but only ask for the 'q' at the very beginning. then attempted to add the actual script inside the quit loop and only draws an snow flake then ask again if I want to exit.... Any idea how to make this work?

terdon avatar
cn flag
Why don't you just use Ctrl+C to exit?
Score:5
jp flag

You can do it like this in the shell bash: Replace the sleep command with a read command, that has a timeout, and let while check for your 'q'. See

help read | less

Here is my version of your snow-show,

#!/bin/bash

LINES=$(tput lines)
COLUMNS=$(tput cols)

declare -A snowflakes
declare -A lastflakes
 
clear

function move_flake() {    

i="$1"
 
if [ "${snowflakes[$i]}" = "" ] || [ "${snowflakes[$i]}" = "$LINES" ]; then
snowflakes[$i]=0
else
if [ "${lastflakes[$i]}" != "" ]; then
printf "\033[%s;%sH \033[1;1H " ${lastflakes[$i]} $i
fi
fi
 
printf "\033[%s;%sH*\033[1;1H" ${snowflakes[$i]} $i
 
lastflakes[$i]=${snowflakes[$i]}
snowflakes[$i]=$((${snowflakes[$i]}+1))
}
 
while [ "$ans" != "q" ]
do
    
 i=$(($RANDOM % $COLUMNS))
 
 move_flake $i

 for x in "${!lastflakes[@]}"
 do
  move_flake "$x"
 done
 read -sn 1 -t 0.1 ans
done
terdon avatar
cn flag
Ha! I didn't know bash's `read` had a timeout option. Nice!
sudodus avatar
jp flag
@terdon, Yes, it is a nice option. I have used it several times in my scripts :-)
WinEunuuchs2Unix avatar
in flag
@terdon I'll have to read up more on the `read` command too :P
Erniemenendez avatar
ag flag
FANTASTIC!!! Thank you very much!!!!
sudodus avatar
jp flag
You are welcome @Erniemenendez :-) I enjoyed the snowing screen, used to to prepare for the winter ... made it fully fullscreen with `xterm`: `xterm -fullscreen -e bash -c 'sleep 1;./snow'` from the directory where I put the script. If you let the snow melt on the ground, it will be a simple but nice screensaver.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.