49

I have a script that I am making and I have an issue with it: I would like to pause execution and wait for user input. I thought that I had it with the read -p -n 1 $foo command but the system is having issues with this command. Here is my current script:

#!/bin/sh

# Ititialization

mainmenu () {
  echo "Press 1 to update your system"
  echo "Press 2 to install samba"
  echo "Press 3 to install vsFTPd"
  echo "Press 4 to install the current version of Webmin"
  echo "Press 5 to configure samba for Active Directory"
  echo "Press x to exit the script"
  read -n 1 -p "Input Selection:" mainmenuinput
  if [ "$mainmenuinput" = "1" ]; then
            updatesystem
        elif [ "$mainmenuinput" = "2" ]; then
            installsamba
        elif [ "$mainmenuinput" = "3" ]; then
            installvsftpd
        elif [ "$mainmenuinput" = "4" ]; then
            installwebmin
        elif [ "$mainmenuinput" = "5" ]; then
            configuresambaforactivedirectory
        elif [ "$mainmenuinput" = "x" ];then
            quitprogram
        elif [ "$mainmenuinput" = "X" ];then
            quitprogram
        else
            echo "You have entered an invallid selection!"
            echo "Please try again!"
            echo ""
            echo "Press any key to continue..."
            read -n 1
            clear
            mainmenu
        fi
}

# This builds the main menu and routs the user to the function selected.

mainmenu

# This executes the main menu function.
# Let the fun begin!!!! WOOT WOOT!!!!

You may notice at the mainmenu function the read -n 1 -p "text goes here" entry. That is where I am having the issue according to ubuntu. Can somebody tell me what is going wrong? thanks!

2
  • 4
    The shebang is wrong. You're using bash features, so the shebang must be set to #!/usr/bin/env bash or #!/bin/bash.
    – geirha
    Apr 13, 2014 at 12:01
  • Please also consider this Unix.SE question of mine, where read and sh (instead of bash) were also causing trouble.
    – Cadoiz
    Nov 5, 2021 at 13:25

1 Answer 1

70

Should be:

read  -n 1 -p "Input Selection:" mainmenuinput

Need to put the n flag after, as that is is telling read to execute after N characters are entered, do not wait for an entire line. Check help read and this for details.

5
  • 2
    I have figured it out! Here is the correct code: read -n 1 -p "Input Selection:" "mainmenuinput" It now does not wait for the press of the enter/return key :-) Apr 13, 2014 at 17:05
  • 2
    "illegal option -n"
    – ses
    May 14, 2014 at 20:06
  • 3
    If you just want to pause execution and wait to continue in a for loop: for $whatever; do $whatever; read -n 1 -p Continue?; done
    – niken
    Feb 2, 2017 at 17:00
  • @ses See my comment here
    – Cadoiz
    Nov 5, 2021 at 13:29
  • If you want to hide the typed character you can add the -s option.
    – schlicki
    Jun 14 at 11:21

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.