In bash, read to a variable with a default value to edit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In bash, read to a variable with a default value to edit
# 1  
Old 12-13-2007
In bash, read to a variable with a default value to edit

My script needs to read a variable from the user. But before the user types the input, I want to give a default value so the user can edit the default value for input.

How can I implement it?

My script is something like:

#!/bin/sh

read -p 'Port number: ' -e port_number

echo "Port number is $port_number"

But, I don't know how to put the default value, say 9900, on the prompt line for the user to edit.

Can anyone help?

Thanks.
# 2  
Old 12-14-2007
This is not real easy, especially if you want a lot of editing features. I don't think it's really worth the trouble. But here is a start..
Code:
#! /usr/local/bin/bash

exec 4>/dev/tty

function readnum
{
        local prompt=$1
        local init=$2
        local backspace=$(echo -e \\b\\c)
        local enter=$(echo -e \\r\\c)
        local savesetting=$(stty -g)
        local keystroke data n i rest reading result
        n=0
        while (( ${#init})) ; do
                rest=${init#?}
                data[n]=${init%$rest}
                init=$rest
                ((n=n+1))
        done
        echo -e "${prompt}"\\c >&4
        while  ((i<n)) ; do
                echo -e ${data[i]}\\c >&4
                ((i=i+1))
        done
        stty -echo -icrnl -icanon min 1 time 0
        reading=1
        while ((reading)) ; do
                keystroke=$(dd bs=1 count=1 2>/dev/null)
                case $keystroke in
                $enter)
                        reading=0
                        ;;
                $backspace)
                        if ((n)) ; then
                                echo -e "${backspace} ${backspace}"\\c >&4
                                ((n=n-1))
                        fi
                        ;;
                *)
                        echo -e ${keystroke}\\c >&4
                        data[n]=$keystroke
                        ((n=n+1))
                esac
        done
        stty "$savesetting"
        echo >&4
        result=""
        i=0
        while  ((i<n)) ; do
                result="${result}${data[i]}"
                ((i=i+1))
        done
        echo $result
        return 0
}

final=$(readnum "well? " 0123)
echo the final number is $final
exit 0

# 3  
Old 12-14-2007
# 4  
Old 12-14-2007
Hi.

Here is a cheap way of doing this:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate setting default value for read variable.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash

echo

default=9900


read -p "Port number (default is $default): " -e t1
if [ -n "$t1" ]
then
  port_number="$t1"
else
  port_number="$default"
fi

echo
echo " Port number is \"$port_number\""

exit 0

Producing (partial copy of interactive session):
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0

Port number (default is 9900):

 Port number is "9900"
leap user-problem/220 % ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0

Port number (default is 9900): 2001

 Port number is "2001"

This doesn't allow "editing" possibly in the sense you are thinking of, but 1) it puts the default value in the prompt, 2) one can enter a bare RETURN to accept the default value, or 3) enter a new value. Beyond this, as Perderabo noted and as you will see from porter's readline reference, things get complicated ... cheers, drl
# 5  
Old 12-14-2007
Quote:
Originally Posted by porter
Thanks all!

Perderabo: very nice script.

porter: I wrote a C program using the readline library, but I still don't know how to put the default value after the prompt so the user can edit on the default value. Any idea? Thanks.
# 6  
Old 12-14-2007
Thanks all!

Perderabo: very nice script.

porter: I wrote a C program using the readline library, but I still don't know how to put the default value after the prompt so the user can edit on the default value. Any idea? Thanks.
# 7  
Old 12-14-2007
Quote:
Originally Posted by pankai
porter: I wrote a C program using the readline library, but I still don't know how to put the default value after the prompt so the user can edit on the default value. Any idea? Thanks.
Well done, well you officially now know more than me about readline. Are there any examples, documentation with the readline package?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using read to assign value to bash variable not working

Hi, I am attempting to assign the output of the following command, to two bash variables, var1 and var2 using "read," but it doesn't seem to be working. # openstack hypervisor stats show | awk -F'|' 'NR==14{print $2,$3}' vcpus 92 # echo $? 0 # openstack hypervisor... (4 Replies)
Discussion started by: sand1234
4 Replies

2. UNIX for Advanced & Expert Users

[BASH] Read value of variable, but not comment on the same line

Heyas So while i'm trying to increase security/usability of my TUI commands, i'm currently at tui-conf-get. There was also a bug, i wanted to fix, but sadly that bugfix is more insecure - but twice as fast as my current buggy method. I've added a getopts toggle, '-s' to toggle beSecure,... (8 Replies)
Discussion started by: sea
8 Replies

3. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

4. Shell Programming and Scripting

scan and edit in bash

so assume I have a dozen files in local directory and half of them are .txt and I only want to scan these text files and go inside each of them and replace absolute paths (e.g. C:\blabla\more blahblah\myfile.txt) with just the name of that file (myfile.txt) and then go to next line and look if... (6 Replies)
Discussion started by: Jaymz
6 Replies

5. 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

6. Shell Programming and Scripting

Edit csv file with bash

I' trying to use bash to edit a csv file from this format: "apples","oranges","grapes" "bread","butter","milk" To this: "apples oranges grapes" So that if I would open the csv file in open office, "apples oranges grapes" Would be in one single cell, and "bread (2 Replies)
Discussion started by: locoroco
2 Replies

7. Shell Programming and Scripting

Read and edit multiple files using a while loop

Hi all, I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands: :1,$s/search_str/replace_str/g<return> but I am not sure how to automate the <return> of these vis commands when I am putting this in a... (8 Replies)
Discussion started by: cyberfrog
8 Replies

8. UNIX Desktop Questions & Answers

grub, ok, bash, edit, kmdb and what else?

Hello There is a > prompt at Grub, # prompt for the console and $ for bash, but I am clueless about when and how to get into a specific prompt, how to move around between one prompt to another and how to exit. Is there a very basic guide anywhere that CLEARLY explains the type of shell... (2 Replies)
Discussion started by: mani1413
2 Replies

9. Red Hat

how to edit the bash shell?

Hello friends, I want to add some features to the bash shell.I logged in as the root.Even then could not view the source code of bash shell in RH 9,Fedora 8.It is in encrypted form.Can you please tell me how to include my code into bash shell. Please give me a sample code so that I can understand... (3 Replies)
Discussion started by: nsharath
3 Replies

10. Linux

How to edit /etc/fstab when root was mounted as read only

can somebody help me out in editing the /etc/fstab. I am on RHEL5 (Tikanga). **The problem is that i have given a wrong LABEL in /etc/fstab for root volume and so after reboot, it is unable to resolve the wrong LABEL; so, i have to edit the /etc/fstab :-( -ilan (3 Replies)
Discussion started by: ilan
3 Replies
Login or Register to Ask a Question