0

Still working on my Prac. Thanks for the help for my previous question.

we are tasked with making a "screensaver" for a terminal window.

what i'm trying to do is while waiting for a key press, randomly generate a number which then picks one of several ASCII art pictures i will have added to the file.

what i have come up with so far kind of works but it will only regenerate another number after i press a key. I'm wanting it to keep generating a number until i press a key and it then dumps me out of the loop.

while read -r -n1 key
do
    num=$(awk -v min=5 -v max=10 'BEGIN{srand(); print int(min+rand()*(max- 
min+1))}')
echo $num
case $num in
1)
    echo "hello"
    sleep 5
    ;;
2)
    echo "bye"
    sleep 5
    ;;
***and so on***
esac
done

i'm sure I've overlooked something simple. been in this chair for most of 7 hours today.

many thanks

EDIT: found this Output something (in a loop) until a key is pressed

and have adapted it accordingly but i don't get a cursor back until last sleep 5 has finished running.

#!/bin/bash

while true; do

num=$(awk -v min=5 -v max=10 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')

case $num in
  1)
    echo "hello"
    sleep 5
    ;;

  *)
    echo "whats going on 'ere?"
    sleep 5 &
    wait $!
    ;;
esac


# In the following line -t for timeout, -N for just 1 character
  read -t 0.25 -N 1 input
  if [[ $input = "q" ]] || [[ $input = "Q" ]]; then
# The following line is for the prompt to appear on a new line.
    echo
    break 
  fi
done
0

Got it working. Ended up with this

#!/bin/bash
tput civis
while true; do

num=$(awk -v min=5 -v max=10 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')

case $num in
  1)
    clear
    cat ./s0.file
    sleep 3
    ;;
....
  *)
    clear
    cat ./s9.file
    sleep 3
    ;;
esac 


# In the following line -t for timeout, -N for just 1 character
  read -t 0.25 -N 1 input
  if [[ $input = " " ]] || [[ $input = " " ]]; then
# The following line is for the prompt to appear on a new line.
    echo
    break 
  fi
done
tput cnorm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.