LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2013, 07:27 AM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
Question Control characters in Shell mode (left, right, up and down arrow keys)


I am writing a console mode shell scipt.I am trying to read a char in my shell script
test.sh

echo "Enter the word"
read ch
echo $ch

Output
-------
[Unix@myunix ~]$ ./test.sh
Enter the word
Hello wonld^[[D^[[D

Issue: Here u see i miss spelled Hello world as Hello wonld.

I am trying to use left arrow keys to go back to the characted 'n' and change it to 'r'. Here the left arrow keys are pressed 2 times. It is displaying ^[[D instead of going back one character.

Can you pls let me know what changes i need to make to work as i expect.

I am using "GNU bash, version 4.2.39(1)-release"

Thanks and Regards,
Jagadish
 
Old 03-27-2013, 08:45 AM   #2
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
echo only displays the string you entered, it doesn't allow you to edit it.

man echo
 
Old 03-27-2013, 08:52 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
I m not echoeing...I m trying to edit the text in read mode...
 
Old 03-27-2013, 09:14 AM   #4
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
Oops, sorry for the confusion. Anyways, 'read' only allows you to add or remove characters in the string to be entered (no real editing capabilities). Left and right arrows are interpreted as combinations of ascii characters. Still, using the backspace key is ok.

http://wiki.bash-hackers.org/commands/builtin/read

Last edited by manu-tm; 03-27-2013 at 09:38 AM.
 
Old 03-27-2013, 09:28 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
In that case, if you have typed something wrong, you should use Unix based keyboard shorcuts like:
Quote:
CTRL+h ......... To remove characters one by one from cursor position
CTRL+u ......... To remove everything that you've typed. It's undo
Quote:
Originally Posted by manu-tm View Post
echo only displays the string you entered, it doesn't allow you to edit it.

man echo
OP has typed what echo command is asking for through read, and that can be erased easily using CTRL+u or CTRL+h.

Last edited by shivaa; 03-27-2013 at 09:36 AM.
 
Old 03-27-2013, 09:52 AM   #6
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
(Damn! Yet I know pretty well that I should never ever attempt to answer before my safe caffeine level is reached...)

Last edited by manu-tm; 03-27-2013 at 11:47 AM.
 
Old 03-27-2013, 11:25 AM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Bash read has a -e option; it uses readline to obtain the line if the shell is interactive. Try
Code:
read -e ch
 
Old 03-28-2013, 12:38 AM   #8
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Thanks,


read -e ch

works, actually i am developing a console menu using cmmds tput cup I am designing it.

It looks like something like this


1.Name :
2.Address :
3.Phone :


When I trying editing something it crosses my variables and i can reach till Left most part and even I can write words on the screen like Water.


Water 1.Name :Tiger
2.Address :
3.Phone :


I dont want it to cross ':' when in editable mode. It should stop at T of Tiger when left arrow keys are used and should not allow to traverse further.
 
Old 03-28-2013, 01:35 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Let's know what exactly you're doing and what you've done. In the meantime you can try with following, which do not cross ':' and stops when it reaches to ': '

Code:
echo -e "Name: \n"; read name
echo -e "Address: \n"; read addr
echo -e "Phone: \n"; read phone
Please use [code][code] around commands output that you're sharing. This option is available under menu when you Reply to Thread.
 
Old 03-28-2013, 02:00 AM   #10
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Try using the -p option to stop the cursor going to the beginning of the line.
Code:
read -ep '1.Name : ' ch

Last edited by Kenhelm; 03-28-2013 at 02:02 AM.
 
Old 03-28-2013, 04:44 AM   #11
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Thanks

I am desiging a console

And my menu looks something like this

1.Name :
2.Address :
3.Phone :


Tput cup etc commands I am using to design the screen, read -ep works

but i am doing

read -ep ch

where 'ch' would be the input that I would be reading

1.Name :
2.Address :
3.Phone :

It works but i get ch character defaulty

1.Name :ch
2.Address :
3.Phone :
 
Old 03-28-2013, 04:54 AM   #12
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Also now the backspace and delete takes the cursor to left most row and it overwrites the console .
 
Old 03-28-2013, 07:06 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
First of all, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Next, no, it's not "read -ep ch". "-p" is the "prompt" option, the text that follows it will appear to the left of the actual typing area. The prompt text will not be saved to the variable, so you'll have to append it later if you need it.

Code:
read -re -p "Please enter your name: ' name
read -re -p "Please enter your address: ' address
read -re -p "Please enter your phone number: ' phone

echo 'Your information:'
echo "Name=$name"
echo "Address=$address"
echo "Phone No=$phone"
There's no way inside the script to control what goes on inside the edit box while it's in use. You can only pre-insert text and/or edit it after you get it.

One thing that most coders do in this situation is put the read inside a perpetual loop, and only break out of it when the reply matches some preset condition.

Code:
while true; do

    read -re -p "Please enter your name: ' name

    case $name in
        *[^[:alnum:]]*) echo "The name must only contain letters or numbers.  Try again." ;;
        *) break  ;;
    esac

done
 
Old 04-01-2013, 12:44 AM   #14
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
I am displaying the Name and all using tput and cup now I only wanna read

Code:
  if [ "$rec_flag" == "N" ]
        then
                while [ "$chk_flag" != "Y" ]
                do
                        tput cup $rec_ind `expr $col_no + 2`
                        read -re -p ch


the code read -re -p ch doesnt store the input in ch instead it displays the the variable ch on screen.
 
Old 04-01-2013, 05:34 AM   #15
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Quote:
the code read -re -p ch doesnt store the input in ch instead it displays the the variable ch on screen.
The -p option expects some text after it. The first parameter after -p is seen as text. It is displayed as the read prompt.
Code:
read -re -p ch                # wrong; ch is seen as text
read -re -p "some_text" ch    # correct; ch is now the read variable
Quote:
I am displaying the Name and all using tput and cup now I only wanna read
In that case, continue using tput and cup to display your prompts but without the ' :'
1.Name
2.Address
3.Phone
then put in the ' :' with read -p
Code:
read -re -p " :" ch
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Left and Right arrow keys do not work on Fedora 16 x64 KDE Janek566 Fedora 2 02-10-2012 07:21 AM
screen / screenrc: how to use the right / ctrl arrow to go left and right in windows? frenchn00b Linux - General 6 11-10-2009 02:11 PM
holding down left and down arrow does not repeat dave201 Linux - General 1 11-22-2008 05:19 PM
Shift-arrow and Alt-arrow keys don't work in xterm Aviv Hurvitz Linux - General 2 09-30-2006 04:43 PM
arrow keys in vi insert mode sanjeev.naik Linux - Software 6 09-04-2004 01:21 PM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration