LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 11:54 AM   #1
woodson2
Member
 
Registered: Oct 2008
Posts: 51

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, 12:58 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

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

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, 10:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 11:18 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 11:29 AM.
 
  


Reply



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 12:31 PM
Cannot prompt user in while-read BlueSpirit Programming 4 05-15-2011 10:28 AM
Bash - Read user input: URLs without the enter key stroke make Programming 2 09-23-2010 04:29 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Editable input from the terminal and redirection neville310 Programming 4 04-10-2007 03:24 AM

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

All times are GMT -5. The time now is 10:08 PM.

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
Open Source Consulting | Domain Registration