/dev/kev

readpreprompt

This is a simple program to read a string of text from the user. The difference between this program and, say, the bash builtin "read" command, is that this program allows you to specify a string of text that is initially presented to the user.

Dependencies: GNU readline

Usage:
     readpreprompt <prompt> [initial_text]
where:
     <prompt> is the prompt to show the user. This is required, if you do not want a prompt, use "".
     [initial_text] is the (optional) initial text to present to the user.

The prompt and user editing is done on stderr, and the user's input is printed to stdout (without trailing newline) when the user hits enter.

Download the readpreprompt-0.1.tar.gz source code

I use this utility to allow me to run a command such as "mv file", and then have the system prompt me for the new filename, using "file" as the default value. This makes renaming long complicated filenames much easier, particularly when I just want to add ".old" to the end (or similar). To do this, I have the following in my .bash_profile file:

MV_OPTIONS="-i"
export MV_OPTIONS
function mv {
	local fname
	if [ $# = 1 ]; then
		fname="$(readpreprompt "New filename: " "$1")"
		command mv $MV_OPTIONS "$1" "$fname"
	else
		command mv $MV_OPTIONS "$@"
	fi
}
export -f mv

Eventually, I hope to patch the bash source code so that the "read" builtin command accepts an option to specify the initial text.

Kevin Pulo, kev at pulo dot com dot au


Last updated: Wednesday, 28 June, 2006.
Copyright © 1994-2018, Kevin Pulo, kev at pulo dot com dot au
Public key fingerprint: 94A4 D2B6 85E6 A46A 5330 74F3 199C 4F85 563D C85F