You are not logged in.

#1 2012-02-28 17:31:20

stevepa
Member
From: Texas USA
Registered: 2012-02-12
Posts: 113

Bash script user input and revision technique requested

In a bash script, I have a need to present the contents of a variable to the user, so that he can "revise" the value if needed, or just hit enter if no revision is required. The objective is to reduce keyboard effort to just revising a presented value.

The "hit enter to continue" part is easy.
The "revise" part is the problem for me.

To illustrate using an example:

#! /bin/bash
position_var=30.39
echo -n "Revise Position if required:${position_var}"
#then some kind of read position_var ??
...


How can I make the position_var available to the user to edit and resubmit?
Thanks
steve.

Last edited by stevepa (2012-02-28 17:31:56)


Arch - LVM - ext4 - gnome (T60p 14.1 1400p  x86_64), (T60 15 flexview 1400p i686)

Offline

#2 2012-02-28 17:39:19

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Bash script user input and revision technique requested

This should do it.  Although it doesn't allow them to *edit* the existing value, it does allow them to accept it or enter a new one.

I don't know an easy way to have them actually edit the value - I can think of a rather roundabout way to do it if needed, but see if the following does what you need.

#! /bin/bash
position_var=30.39
read -p "Revise Position if required (current: $position_var): " new_position
[[ "$new_position" != "" ]] && position_var="$new_position"

# do something with your position_var

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-02-28 18:02:12

Blµb
Member
Registered: 2008-02-10
Posts: 224

Re: Bash script user input and revision technique requested

The above solution is good okay for a general shell script, bash has a feature for what you want though. (see below)
If however the content of the variable is bigger (multiple lines, or a really LONG line you don't want to have to retype), you have 2 options:
Write it to a temp file and fire up $EDITOR (if that's not set, you can default to nano or vi, whichever you can find with $(which ...)).

The other idea that comes to mind is using 'expect' to present a prefilled prompt the user can edit... I'll have to test this...
EDIT: Can't think of an easy way to do that with expect... mhh...

EDIT2: Specifically for bash:

read -p "Revise Position if required: " -i "$current" -e new

with the -e flag, $current will be what the line is prefilled with, and $new will contain the result.

Last edited by Blµb (2012-02-28 18:14:55)


You know you're paranoid when you start thinking random letters while typing a password.
A good post about vim
Python has no multithreading.

Offline

#4 2012-02-28 20:16:47

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Bash script user input and revision technique requested

Cool ... I wasn't familiar with the `-i -e` options.  That's definitely a better way to do it.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB