Using read to prompt for editable user input in Bash 3


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using read to prompt for editable user input in Bash 3
# 1  
Old 11-27-2012
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
# 2  
Old 11-28-2012
A simpler portable way to do the same thing is:
Code:
#!/bin/bash
default=user1
printf "Please enter the username you would like to add (%s): " "$default"
read input
USERNAME=${input:-$default}
echo USERNAME is $USERNAME

It should work with any version of bash, ksh, or any other POSIX conforming shell.
The only portable option to the read utility is -r.

Note that I used printf instead of echo because the way you specify printing a prompt without a newline using echo varies from system to system.
# 3  
Old 11-28-2012
Thanks for this code.

Although it's not quite what I'm looking for it's definitely a more portable way of doing things.

The code you proposed results in the following output:

Please enter the username you would like to add to LDAP (user1):



The code I use in BASH 4 results in the following output:

Please enter the username you would like to add: user1


The user name is populated on the editable line already so I can hit enter and accept or hit backspace and make the changes I want.


It's really semantics but It can save a few key strokes.
# 4  
Old 11-28-2012
Hitting enter for the default value is what I tend to see done in these situations.

The other way, beyond bringing an external utility with you, would be to play funny games with the terminal device and handle all keystrokes by yourself:

Code:
#!/bin/bash

oldsettings=$(stty -g)

trap "stty $oldsettings" EXIT

readdefault() { # variable prompt default
        local POS="${#3}"
        local VALUE="$3"
        local C

        stty -icanon min 1 time 0 -icrnl -echo

        printf "%s" "$2$3"

        while true
        do
                C="$(dd bs=10 count=1 2> /dev/null)"

                [ -z "$C" ] && continue

                # Okay, you've got raw keystrokes.  Now you're on your own.
        done

        stty icanon
}

readdefault VAR "Enter something:" "qwerty"

I tried filling out more, but could not for the life of me get my terminal and tput to agree on what the 'backspace' key is, even though backspace works in my terminal.

Example partly from here.

You can see why what you're asking for really isn't trivial. It may be more straightforward to upgrade your shell.

Last edited by Corona688; 11-28-2012 at 01:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

Perl to read user input

I am creating a bash that uses perl . The below code closes before the input is entered. If I run the perl as a .pl it is fine. What am I doing wrong? Thank you :). #!/bin/bash cd 'C:\Users\cmccabe\Desktop\wget' wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv print... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

BASH - read does not wait for user input in some circumstances

Hello. I am running 2 scripts : script_1 and script_2 These scripts are run as root Script 2 contains : #!/bin/bash # # ~/bin/script_2 # E_BAD_PARAM=115 # date2stamp () { date --date "$1" +%Y-%m-%d___%H:%M:%S } # USER_NAME=$1 NB_PARAM=$# PARAM0=$0 (2 Replies)
Discussion started by: jcdole
2 Replies

4. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

5. Shell Programming and Scripting

Help with Bash user input

I am starting to learn how to use bash and I would like the script to do the following: Asks the user for his/her name Asks the user for one number Asks the user for another number Then it adds the two numbers, Also multiply the two numbers I have the part where you get the name, and I... (3 Replies)
Discussion started by: boyboy1212
3 Replies

6. Shell Programming and Scripting

Solaris- Read command from user input

I need to write a bourne shell script (solaris 10) that accepts input from the user. The input will be a command- any command like ls/ pwd/ mv etc. After the input is read, the shell must execute the command supplied by the user. I know we use read to play with user inputs. Just not sure how to... (2 Replies)
Discussion started by: PDManc
2 Replies

7. Shell Programming and Scripting

BASH - read use a path as input

I am trying to script simply data transfer. I would like to have the user input the source "SRC" (/Volumes/DriveName/Users/johnq123) and then name the directory that the copied information will go to, "DST" . put I can't get it to work - #!/bin/bash ... (8 Replies)
Discussion started by: dropkick888
8 Replies

8. Shell Programming and Scripting

Bash user input

Hi all, I currently have a script which uses read -p for user interaction. e.g. read -p "New user? " user Is it possible to have it so if the user enters nothing and just presses return it can resort to a specified value instead? Thanks! :) (5 Replies)
Discussion started by: JayC89
5 Replies

9. UNIX for Dummies Questions & Answers

How to read a line of text from user input?

Hiii I wanna a read a line of text from standard input. The user enter data like this way name phone_no month1_salary month2_salary that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both... (4 Replies)
Discussion started by: krishnampkkm
4 Replies

10. UNIX for Dummies Questions & Answers

getline to read input from a user

Hi, The gcc compiler has warned about using gets(), so I've been trying my hand at getline. Problem is that I've been able to read from a file, but what I really need is to read from a user's input. I want to use getline like a scanf() command, but I can't figure what to substitute for the fp... (6 Replies)
Discussion started by: sdsd
6 Replies
Login or Register to Ask a Question