LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2014, 12:58 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 187
Blog Entries: 1

Rep: Reputation: Disabled
A condition in my script doesn't work!


The second condition in my script should be done by pressing Enter key from STDIN , but it doesn't do any thing when I do it!
Code:
#!/bin/bash
while :
do
        read -r -n1 c
        [ "$c" == "^[" ] && break
        if [ "$c" == "^M" ]; then
                echo "Then name is: $name"
                count+=1
                unset name
        fi
        name="$name$c"
done
echo "^[[33mThe number of names are $count"
echo ^[[0m
 
Old 02-25-2014, 01:25 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,937

Rep: Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110
Quote:
The second condition in my script should be done by pressing Enter key from STDIN
What makes you think that?
What information can you show that says "^M" is what is returned from the enter key being pressed?
 
Old 02-25-2014, 01:33 AM   #3
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 187
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
What makes you think that?
What information can you show that says "^M" is what is returned from the enter key being pressed?
I want to show name variable
 
Old 02-25-2014, 01:54 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,937

Rep: Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110
I understand you wish for the 'if' to return true, but my question is, how is your current test accurate?
Where are you getting the information from that "^M" is what should be stored in your variable?

I am curious as well, when you are typing the code, what keys are you pressing to enter "^[" and "^M"?
 
Old 02-25-2014, 02:01 AM   #5
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 187
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I understand you wish for the 'if' to return true, but my question is, how is your current test accurate?
Where are you getting the information from that "^M" is what should be stored in your variable?

I am curious as well, when you are typing the code, what keys are you pressing to enter "^[" and "^M"?
At first I press ctrl+v(=^) then I press any key that I want, for example Esc(=[); the first condition for Esc key works correctly but the second doesn't!
 
Old 02-25-2014, 03:47 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 780

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
read uses \n as a line delimiter, so, after just pressing enter, nothing is read into c.
You probably want the -N 1 read switch, rather than -n 1. Then the newline character will be read by read, but discarded by word splitting. That can be fixed by setting IFS, though.
 
1 members found this post helpful.
Old 02-25-2014, 04:25 AM   #7
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 187
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
read uses \n as a line delimiter, so, after just pressing enter, nothing is read into c.
You probably want the -N 1 read switch, rather than -n 1. Then the newline character will be read by read, but discarded by word splitting. That can be fixed by setting IFS, though.
Thank you, I have another problem, I exit by pressing Esc but it happens by other keys like as up and down in arrow keys! I want that it happen just by Esc
 
Old 02-25-2014, 04:50 AM   #8
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 6,254

Rep: Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672Reputation: 1672
up and down send key sequences, and the first key in that sequence is an escape. so you need to collect all the keys and evaluate.
 
Old 02-25-2014, 09:45 AM   #9
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 459

Rep: Reputation: 192Reputation: 192
Note that you are comparing $c against a string “^M”, i.e. two character sequence: a caret followed by capital M. This is not the same as new line character, even though when you invoke read it is printed the same way.

Simple way to check that this is the case is to press backspace, as in: run read, press Ctrl+V, press Return and then press Backspace. Both characters (“^M”) will be returned. In this case, “^M” is just a representation of a single control character.

The same is true about “^[” which is representation of a single character.

Frankly, if you need to read control characters and keys such as up and down, shell scripting is not the language you want to use.
 
Old 02-25-2014, 10:18 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,892

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006
As suggested in your other thread use the following for just pressing the enter key.
if [[ $c="" ]]
 
Old 02-27-2014, 04:03 PM   #11
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,157

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
What the OP has displayed are the escape sequences associated with various non-printable ASCII characters. For example, ^[ is how an escape character is often represented, and a carriage return (\r) (HEX x0D) is displayed in the man console_codes output as "^M".

I believe the OP has confused the internal byte-code representation of terminal input with the text input used by bash (and most other high level languages).

<edit>
Those codes can be used in scripts, but only as hex coded strings.

For example, with this file:
Code:
$ cat /usr/share/bash/define_colors
####################################################################################
#
# Console code definitions (foreground colors only)
#
# See man console_codes for a full description
#
####################################################################################
define_colors()
{
  csi=$'\x1B['
  reset="${csi}0m"
  bold="${csi}1m"
  black="${csi}30m"
  red="${csi}31m"
  green="${csi}32m"
  brown="${csi}33m"
  blue="${csi}34m"
  magenta="${csi}35m"
  cyan="${csi}36m"
  white="${csi}37m"
  bold_red="${bold}${red}"
  bold_green="${bold}${green}"
  bold_brown="${bold}${brown}"
  bold_blue="${bold}${blue}"
  bold_magenta="${bold}${magenta}"
  bold_cyan="${bold}${cyan}"
  underscore_on="${csi}38m"
  underscore_off="${csi}39m"
}
The following script:
Code:
$ cat color_test 
#!/bin/bash
. /usr/share/bash/define_colors
define_colors
echo "${red}Colour=red${reset}"
produces this output
Code:
Colour=red

Last edited by PTrenholme; 02-27-2014 at 04:33 PM.
 
Old 02-28-2014, 08:44 AM   #12
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 459

Rep: Reputation: 192Reputation: 192
Quote:
Originally Posted by PTrenholme View Post
Those codes can be used in scripts, but only as hex coded strings.
The “but only” part of the sentence is not correct. One can include those variables in the script file (if ones editor allows for that or there's some other method for doing so). Readability of such a method is left for the reader to judge:
Code:
$ cat a.sh
#!/bin/sh
echo 'red'
$ cat -v a.sh
#!/bin/sh
echo '^[[31mred^[[0m'
$ sh a.sh
red
$
It has the advantage of not requiring bash extensions (namely $'…'), but at either case, for achieving colours, tput utility might be the best choice anyway.
 
Old 02-28-2014, 04:24 PM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,157

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Quote:
Originally Posted by mina86 View Post
The “but only” part of the sentence is not correct.[...]
Yes, you're quite correct.

However, the point of my post was not to hijack this thread into a discussion of how to use those codes. I was trying to gently suggest to the O.P. that an examination of some actual bash scripts, or reading some tutorials, or both, might be helpful in understanding the problem with the test(s) being used in the code posted above.

Just searching this sub-forum for posts containing bash should yield several interesting and helpful examples that could be studied.
 
Old 03-18-2014, 07:42 AM   #14
Sandcrawler
LQ Newbie
 
Registered: Feb 2014
Distribution: CentOS
Posts: 9

Rep: Reputation: Disabled
Code:
#!/bin/bash
while :
do
        read -r -N1 c
        [ "$c" == "^[" ] && break
        if [ "$c" == "^@" ]; then
                echo "Then name is: $name"
                count+=1
                unset name
        fi
        name="$name$c"
done
echo "^[[33mThe number of names are $count"
echo ^[[0m
try this - works for me..
hold Ctrl+v+enter (do not release ctrl+v before pressing enter)


Assume you are looking for output something like this:

# ./do_stuff
Then name is:
boo
Then name is: boo
hello
Then name is: hello

Last edited by Sandcrawler; 03-18-2014 at 07:44 AM.
 
  


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
[SOLVED] cd command doesn't work in my script xeechou Linux - Newbie 5 07-12-2013 08:11 AM
Script on NIC Up - Doesn't Work Quantumstate Linux - Networking 2 01-19-2012 10:23 AM
[SOLVED] rm at bottom of script doesn't work qwertyjjj Linux - Newbie 7 08-18-2009 12:03 PM
script doesn't work during startup of the pc shipon_97 Linux - Enterprise 3 04-30-2007 12:38 PM
'export' from a script doesn't work, why? sylvain_gnu Debian 3 04-20-2004 12:42 PM


All times are GMT -5. The time now is 04:07 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