DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Mariusz has posted 3 posts at DZone. View Full User Profile

Get Input From Terminal With Prompt, History, Line Editor

07.27.2008
| 5971 views |
  • submit to reddit
        Using this script you will be able to read user input using readline goodies but without need to manually program readline library. Just save it under e.g. /usr/local/bin/getline, and then call like:
getline {prompt} {historydir}.

for example:
getline "input> " "/tmp"

You can for example call this script from php using popen(). This way php doesn't have to include readline module, and you can do shell stuff not inside php, but using shell code, which is more natural and comfortable way to do it.

#!/bin/bash -f
history -r "$2/.getline_history"
LINE=""
read -re -p "$1" LINE
history -s "$LINE"
history -w "$2/.getline_history"
echo $LINE