Find the answer to your Linux question:
Results 1 to 4 of 4
Hello all, I'm writing a script that prompts user to insert a blank cd to the cd drive and ask user to hit enter when done, otherwise the the script ...
  1. #1
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251

    script waiting for user to hit enter

    Hello all,
    I'm writing a script that prompts user to insert a blank cd to the cd drive and ask user to hit enter when done, otherwise the the script will beep every second, what I came up with so far is

    Code:
    #!/bin/bash
    while true; do
      echo "Insert a blank cd, hit enter to continue"
      echo -en '\a' > /dev/console
      read -n 1 keypress
      if [ "$keypress" == "" ]; then
        break
      fi
      sleep 1
    done
    #to run rest of the code
    The problem with this script is that it beeps only once, then waits for user's input. I think the line in bold is causing the problem because it won't exit until something is entered. Is there a way to fix this? Any suggestions are welcome. I'm running OpenSuSE 10.3.

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    1) type "help read"
    2) understand what you are reading
    3) notice the option "-t" for "timeout"
    4) success!

  3. #3
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Hey burschik,
    I should have read the manual for read command more carefully. Thanks for the direction. I've solved the problem with the following code:

    Code:
    #!/bin/bash
    [ ! `whoami` == "root" ] && echo "must be root to run this script" && exit 1
    keypress="a"
    echo "Insert a blank cd, hit enter to continue"
    until [[ $keypress == '' ]]; do
      echo -en '\a' > /dev/console
      read -t 1 -n 1 keypress
    done

  4. #4
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    You're welcome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...