0

I have a while loop, I would like to stopped it when the user presses the letter "s" on the keyboard.

I should manage the problem with a "trap" and using relative signal, which by default block has with combination "ctrl + c", but I should have the same effect by pressing "s".

(I do not have to use the command read)

Where to place the commands?

could you explain me ?

Thank you

The code:

!/bin/bash

while true

do

echo text text text text

done
compup
  • 77
  • 1
  • 8
  • Could you paste a code sample ? How fast is an iteration of your `while` loop ? – Aserre Mar 08 '18 at 16:31
  • Welcome to Stack Overflow! Please update your question to show what you have already tried in a [minimal, complete, and verifiable example](https://meta.stackoverflow.com/questions/261592) and add sample input and expected output. For further information, please see [how to ask good questions](https://stackoverflow.com/help/how-to-ask), and take the [tour of the site](https://stackoverflow.com/tour) :) – Gilles Quénot Mar 08 '18 at 16:39
  • Simply: !/bin/bash while true do echo text text text text done – compup Mar 08 '18 at 16:47

2 Answers2

0

Following what Aserre says:

#!/bin/bash

if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi

keypress=''
while [ "$keypress" != "s" ]; do
  echo text text text
  keypress="`cat -v`"
done

if [ -t 0 ]; then stty sane; fi
Pierre François
  • 5,850
  • 1
  • 17
  • 38
0

I don't know why you don't want to use read as it would be as simple as

#! /bin/bash

while :
do
    echo "text text text"
    read -t 0.3 -n 1 k
    [[ "$k" == 's' ]] && break
done
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134