LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 06: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, 07: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, 07: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, 08: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 08:38 AM.
 
Old 03-27-2013, 08: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 08:36 AM.
 
Old 03-27-2013, 08: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 10:47 AM.
 
Old 03-27-2013, 10:25 AM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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-27-2013, 11:38 PM   #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, 12: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, 01:00 AM   #10
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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 01:02 AM.
 
Old 03-28-2013, 03: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, 03: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, 06:06 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 03-31-2013, 11:44 PM   #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, 04:34 AM   #15
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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 06: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 01:11 PM
holding down left and down arrow does not repeat dave201 Linux - General 1 11-22-2008 04:19 PM
Shift-arrow and Alt-arrow keys don't work in xterm Aviv Hurvitz Linux - General 2 09-30-2006 03:43 PM
arrow keys in vi insert mode sanjeev.naik Linux - Software 6 09-04-2004 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:11 AM.

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
Open Source Consulting | Domain Registration