[Bash] Read History function & Read Arrowkeys


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Bash] Read History function & Read Arrowkeys
# 1  
Old 12-07-2014
[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.
# 2  
Old 12-07-2014
Bash has an -e option to read to support READLINE functionality (emac/vi editing, history, etc)

So try:

Code:
read -e -p "Enter your name: " name

To add responses to the history use history -s <response>

Example:

Code:
# Clear history
history -c

while true
do
    read -e -p "Enter your name (QUIT when done):" name
    #Save name into history
    history -s "$name"
    [ "$name" = "QUIT" ] && break
    echo "Hello, $name"
done

This history can also be written out to a file (for later invocations of your script) with history -w <filename> and read back in from a file with history -r <filename>

Last edited by Chubler_XL; 12-07-2014 at 04:02 PM..
# 3  
Old 12-08-2014
How can I save it in your own history file?
# 4  
Old 12-08-2014
You can save the history to a file (say .username.history) with this command:

Code:
history -w $HOME/.username.hist

To read history back use:

Code:
history -r  $HOME/.username.hist



So our example username prompting script would do this:

Code:
HISTFILE=$HOME/.username.hist
# Clear history
history -c

# If previous history exists load it in now
[ -f "$HISTFILE" ] && history -r "$HISTFILE"

while true
do
    read -e -p "Enter your name (QUIT when done):" name
    #Put name into history
    history -s "$name"
    [ "$name" = "QUIT" ] && break
    echo "Hello, $name"
done

# Save history for next time we run script
history -w "$HISTFILE"

# 5  
Old 12-09-2014
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: How to use read with conditions & loops

Hello, Below I try to control that the input is good an IP : #!/bin/bash cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt chmod 644 /home/scripts/interfaces.txt echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below... (9 Replies)
Discussion started by: Arnaudh78
9 Replies

2. UNIX for Dummies Questions & Answers

MAN and read & write function

How to use MAN to find information about read() and write() function ? The command "man read" show some rubbish, for example "man open" show great information about function I need. (2 Replies)
Discussion started by: bbqtoss
2 Replies

3. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

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

5. Shell Programming and Scripting

bash read within function with arguments

I have trouble getting this logic to work #!/bin/bash function assign_var(){ while do read -p "$2 :" $3 done } assign_var '$IPADDRESS' ipaddress IPADDRESS Basicly, i want to make sure that entry is made (i can add more sophisticated checks later), but the idea is to recycle... (11 Replies)
Discussion started by: serverchief
11 Replies

6. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

7. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

8. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

9. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

10. Shell Programming and Scripting

History for custom BASH function

Hello all, I have a bash function that opens Safari (I'm on OS X) with a specified argument. Here it is : function safari { #Safari bash function TLDS=( "http://www." ".com" ".org" ".net" ".gov" ".edu" ) if ; then open -a Safari ${TLDS}$2${TLDS} elif ; then open -a Safari... (0 Replies)
Discussion started by: inquen
0 Replies
Login or Register to Ask a Question