LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2012, 12:54 PM   #1
woodson2
Member
 
Registered: Oct 2008
Posts: 47

Rep: Reputation: 15
Using read to prompt for editable user input in Bash 3


Below is a simple script to prompt for user input while suggesting an editable default value at the prompt:

Code:
shortname=user1

read -e -i $shortname -p "Please enter the username you would like to add: " input
USERNAME="${input:-$shortname}"

Please enter the username you would like to add: user1
The above is the expected and working output in BASH 4

If I try this same code in BASH 3 it doesn't work because this version does not support the "-i" switch.

I need some help getting the same result in BASH 3.
Thanks
 
Old 11-27-2012, 01:58 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,978
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
What does -i do?
 
Old 11-27-2012, 02:20 PM   #3
woodson2
Member
 
Registered: Oct 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by custangro View Post
What does -i do?
-i text
If readline is being used to read the line, text is placed into the editing buffer before editing begins.
 
Old 11-28-2012, 11:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,642

Rep: Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960
Personally it seems like a bit of an overkill, so my suggestion would be to place your default name in brackets at the end of the prompt as most users would recognise
this to mean if they simply press enter then this will be the value used.
Code:
read -e -p "Please enter the username you would like to add:(user1) " input
 
Old 11-28-2012, 12:18 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
If there's no -i option, then there's simply no way to pre-load the text in the editing buffer, AFAIK. You'll just have to do without it. grail's suggestion of explicitly stating the default that will be used is about all that you can do.

Edit:
-i was added in 4.0-alpha
http://mywiki.wooledge.org/BashFAQ/061

Last edited by David the H.; 11-28-2012 at 12:29 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting with sudo and password prompt, and accepting user input. Jetso Programming 14 07-31-2011 01:31 PM
Cannot prompt user in while-read BlueSpirit Programming 4 05-15-2011 11:28 AM
Bash - Read user input: URLs without the enter key stroke make Programming 2 09-23-2010 05:29 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 07:40 PM
Editable input from the terminal and redirection neville310 Programming 4 04-10-2007 04:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration