1

So I want to wait 1 second for some input (options that i will implement later). Or i want the program to print something(will implement later too). I have run into a problem tho, when trying to read that 1 char for the function, here is my code:

while true
   do read $var -t 1
      case $var in
          ("h")
              help
              ;;
      esac
done

If I try to echo after the case, the program does wait for 1 second, the problem is it doesnt recognise my h input, how would i fix that?

1 Answer 1

1

I've modified your sample slightly so that it works. There was an error in the read statement. use read varinstead of read $var. This corrected sample will now recognise also the h input.

Related to your question Why it doesn't wait the second (which was btw. hard to determine so i increased the timeout a bit ;-) )? This is because when you enter something, the read timeout is interrupted. It is as the parameter name say's a timeout for the user input. So if the user input's something the timeout is interrupted.

#!/bin/bash

while true 
do
      echo 'wait for input ...'

      read -t 10 var

      echo 'got input ...'

      case $var in
        h)
          echo 'help'
        ;;
      esac
done
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, its an assignment for school, it need to list info of the current processes and refresh it every second, also change what it outputs depending on you pressing certain keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.