Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Read user input with bash?

user input

  • Please log in to reply
4 replies to this topic

#1 Crop


    CC Resident

  • Just Joined
  • PipPipPipPip
  • 59 posts

Posted 02 January 2009 - 05:25 PM

I've seen some installation scripts and other such scripts ask for user input (IE: Do you wish to continue? ) but I can't find an example when I now need it. How can I read user input to a variable in bash?

'Do you wish to continue?'

A yes would continue, no would exit. Anyone?
  • 0

#2 Guest_Jordan_*

  • Guest

Posted 05 January 2009 - 06:23 AM

You can use the 'read' function:

read continue
echo '$continue'

More information here: http://tldp.org/LDP/...sect_08_02.html
  • 0

#3 phpforfun


    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1056 posts

Posted 05 January 2009 - 01:28 PM

Why not make it a function :)

echo would you like to continue?
read continue
if [ $continue == yes ]; then
echo "You want to continue..";
echo "You dont want to continue..";

Be sure to put a space between if and [, and a space between the brackets and the words/variables
It will show:

justin@justin-laptop:~/Desktop$ ./if
would you like to continue?
You want to continue..
justin@justin-laptop:~/Desktop$ ./if
would you like to continue?
You dont want to continue..

  • 0

#4 DigitalLinx


    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 28 August 2009 - 12:20 PM

Also you can pass arguments to your script like this.

echo "The first argument is $1"

echo "The second argument is $2"

sh script.sh argument_one argument_two

  • 0

#5 asafe


    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 106 posts

Posted 29 August 2009 - 08:21 AM

One trick of those installation scripts is not need press enter or letting it be yes:
read -s -n1 -p "Do you wish continue?Y/n " key
case $key in
"Y" | "y") # continue ;;
"N" | "n") #quit;;
*) #continue;;

  • 0

Also tagged with one or more of these keywords: user input

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download