Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Read user input with bash?

user input

  • Please log in to reply
4 replies to this topic

#1 Crop

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_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

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1056 posts

Posted 05 January 2009 - 01:28 PM

Why not make it a function :)

#!/bin/bash
echo would you like to continue?
read continue
if [ $continue == yes ]; then
echo "You want to continue..";
else
echo "You dont want to continue..";
fi

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?
yes
You want to continue..
justin@justin-laptop:~/Desktop$ ./if
would you like to continue?
no
You dont want to continue..
justin@justin-laptop:~/Desktop$


  • 0

#4 DigitalLinx

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.
#!/bin/sh

echo "The first argument is $1"

echo "The second argument is $2"

sh script.sh argument_one argument_two

  • 0

#5 asafe

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;;
esac

  • 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