Read a multiline text from a console - supporting arrow keys


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 10-15-2012
Read a multiline text from a console - supporting arrow keys

Hi,

I try to read a multiline text from a console and write it to a file using a bash script. I want to end reading from a console if a user types the [Enter] key twice. I tried this loop:
Code:
while read LINE; do
if [ ! $LINE ] 2>/dev/null; then
   break
fi
echo -e ${LINE} >> $file
done

However, it doesn't support the arrow keys

Do you have any idea?

Thanks in advance
# 2  
Old 10-15-2012
bash

Hi,

Try this one,

Code:
readmultiline.sh

#! /usr/bin/env bash
file=file1
while read LINE; do
if [ -z "$LINE" ]; then
   break
fi
echo -e "${LINE}" >>$file
done

$ bash readmultiline.sh
Unix is god^[[B^[[A^[[C^[[D
Linux^[[A^[[D^[[B^[[C^[[A

$ cat -A file1
Unix is god^[[B^[[A^[[C^[[D$
Linux^[[A^[[D^[[B^[[C^[[A$

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
wenclu (10-15-2012)
# 3  
Old 10-15-2012
Quote:
Originally Posted by wenclu
...
However, it doesn't support the arrow keys
What do you mean? Do you want to skip through earlier input lines with the arrow keys?
This User Gave Thanks to RudiC For This Post:
wenclu (10-15-2012)
# 4  
Old 10-15-2012
Quote:
Originally Posted by wenclu
I want to end reading from a console if a user types the [Enter] key twice.
try this..

Code:
while [[ $a < 2 ]]
do
read line
if [[ "$line" != "" ]]
then
let a=0
echo $line >> file_temp
else
let a++;
fi
done

It will stop if user press two consecutive enters..(Means two consecutive Blank inputs..)Smilie

Last edited by pamu; 10-15-2012 at 05:47 AM..
This User Gave Thanks to pamu For This Post:
wenclu (10-15-2012)
# 5  
Old 10-15-2012
Hi,

Thank you for your replies. However, I didn't express myself clearly, I just want to be able to move a cursor using the arrow keys while reading an input from a console, because now after pressing an arrow key I got e.g. ^[[D^[[C^[[A^[[B output instead of moving my cursor
# 6  
Old 10-15-2012
Again, what's your purpose? Do you want to skip back in your input lines or do you just want to move left and right? The latter can be achieved - in bash, may be unavailable in other shells - by switching on the readline library functions using the -e option: read -e lineThe former is much harder to do, like using numbered variables or an array, and writing to the output file consistently will become a nightmare; you might be better off using an editor.
This User Gave Thanks to RudiC For This Post:
wenclu (10-15-2012)
# 7  
Old 10-15-2012
I just want to move left/right and if it's possible also up and down, but honestly I don't have any idea how to achieve the latter. Anyway, thank you for your solution with the -e option
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Change/Delete Multiline text wenclu Shell Programming and Scripting 1 09-14-2012 09:27 AM
Arrow keys binding in AIX v5.3 - how? alex_5161 UNIX for Dummies Questions & Answers 2 12-22-2011 06:40 PM
Tux-HJKL as arrow keys dotancohen UNIX for Dummies Questions & Answers 2 09-01-2011 04:14 PM
SSH closes connection when using arrow keys stv_t UNIX for Advanced & Expert Users 0 09-17-2009 06:53 AM
problem using arrow keys in bash shell Moumou Shell Programming and Scripting 2 07-17-2009 03:14 AM
Using arrow keys in shell scripts cambridge Shell Programming and Scripting 4 05-26-2009 06:15 AM
Multiline read with multicharacter record seperator pt14 Shell Programming and Scripting 3 02-12-2009 09:38 PM
how to display multiline text mmunir Shell Programming and Scripting 2 02-05-2009 02:56 PM
SunOS 5.10 - VI Arrow keys not working ravashingravi UNIX for Dummies Questions & Answers 7 12-01-2008 10:22 AM
command scrolling using arrow keys hemangi13 UNIX and Linux Applications 4 09-11-2008 12:53 PM
Using arrow keys cooldude Solaris 4 06-13-2008 11:51 AM
How to read arrow keys on really old bash? simonb Shell Programming and Scripting 1 05-01-2006 10:27 AM
how do I get my arrow keys to work for looking thru my PREVIOUS UNIX COMMANDS spotnis UNIX for Dummies Questions & Answers 11 09-14-2005 01:22 AM
arrow keys / special keys raguramtgr UNIX for Dummies Questions & Answers 3 02-20-2004 01:45 AM
Trapping Arrow keys nageshrc Shell Programming and Scripting 1 11-30-2001 02:36 PM