Suggest answer to user input in bash scripting
Asked Answered
F

5

23

Here is an example:

#!/bin/bash
echo -e "Enter IP address: \c"
read
echo $REPLY

But I want to make it easier for the user to answer. I'd like to offer an answer to the user. It should look something like this:

Enter your IP: 192.168.0.4

And the user can just press Enter and agree with 192.168.0.4, or can delete some characters (for example delete "4" with one backspace and type 3 instead).

How to make such an input? It is possible in bash?

Firecrest answered 18/12, 2010 at 20:26 Comment(0)
P
28

bash's read has readline support (Edit: Jonathan Leffler suggests to put the prompt into read as well)

#!/bin/bash
read -p "Enter IP address: " -e -i 192.168.0.4 IP
echo $IP
Panelboard answered 18/12, 2010 at 20:35 Comment(3)
Why not go the whole hog and use: read -p "Enter IP address: " -i 192.168.0.4 -e IP?Thirtyone
Good to know about -i. Might want to abstract out the default IP into a variable as well.Inocenciainoculable
Good to know about combining -e with -i to pre-fill the reply buffer, but note that -i requires Bash 4.0 or higher (OSX users as of OSX 10.10 using the stock Bash version, bash 3.2.57, are out of luck).Alary
I
21

The way I would do this is to suggest the default in the prompt in brackets and then use the default value parameter expansion to set IP to 192.168.0.4 if they just pressed enter, otherwise it will have the value they entered.

#!/bin/bash
default=192.168.0.4
read -p "Enter IP address [$default]: " IP
IP=${IP:-$default}
echo "IP is $IP"

Output

$ ./defip.sh
Enter IP address [192.168.0.4]:
IP is 192.168.0.4

$ ./defip.sh
Enter IP address [192.168.0.4]: 192.168.1.1
IP is 192.168.1.1
Inocenciainoculable answered 18/12, 2010 at 20:34 Comment(2)
Nicely done; this is the next best thing to -e -i if your Bash version is < 4. I suggest using a different variable name in your example, because REPLY has special meaning: it is set by Bash if you do not specify a reply variable name, and its value is set to the raw value entered, including any whitespace.Alary
To me this is much cleaner than -e -i as it clears input from the screen.Lippert
T
8

The classic way to do most of what you want is:

default="192.168.0.4"
echo -e "Enter IP address ($default): \c"
read reply
[ -z "$reply" ] && reply=$default
echo "Using: $reply"

This doesn't give the editing option.

Thirtyone answered 18/12, 2010 at 20:34 Comment(2)
Heh, jinx. Good call on abstracting out the default IP.Inocenciainoculable
This one also works with zsh...Generation
K
1

Editing isn't practical but it's common to do something like:

echo -e "Enter IP address [$default]: \c"
read answer
if [ "$answer" = "" ]; then
     answer="$default"
fi
Khedive answered 18/12, 2010 at 20:35 Comment(1)
Yes. these ways are good but without editing :'(\n \n Why editing is useful for me:\n I have big questionnare. It can happen that user has made some mistakes and user wants to check out his answers. Than all questions are asked to user again but his last answer to every question is suggested automatically. I user made a little mistake (for example in last symbols) - he can edit it quicklyFirecrest
A
0

You can suggest last value if you have a loop:

while [[ $continue != "y" ]]
do
   echo ""
   read -p " - Enter the name : " -e -i "$name" name
   echo ""
   echo " - Chosen name is: $name "
   read -p " - continue (yes) ou retry (no) (y/n):  " continue
done
   echo " - We continue...  "
   echo " - Chosen name is: $name "
Annoy answered 30/3 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.