LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 07-27-2007, 11:22 PM   #1
fanbin
LQ Newbie
 
Registered: Jul 2007
Distribution: fedora 6
Posts: 8

Rep: Reputation: 0
another problem for new guy in bash


I thought this code "read keylog" could read any thing from my keyboard including the enter. If I type the enter key, what will be stored in keylog? I used to think it will be null, but when I use
[ -n keylog ] to judge if it is null, some mistakes happpen, why?
 
Old 07-28-2007, 12:03 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
It's easier to answer your question if you give us something specific to hang our hats on.

Please do this:
  1. Write a tiny, complete bash script which illustrates the problem.
  2. Tell us what you expect it to do.
  3. Tell us what happened instead.

When you post the script for us to see, please put it within CODE markers. The easiest way to do that is as follows:
  1. Click the Go Advanced button at the bottom of your editing window.
  2. In the new editing window, highlight the entire script with your mouse.
  3. Click the # icon at the top of this new editing window.
 
Old 07-28-2007, 12:10 AM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
The 'man bash' documentation will go a long way to explaining things.

Briefly, the 'read' command uses (by default) a newline (the enter key) as a delimiter for the input, so it's not stored in the variable. The test you are looking for is

[ -n "$keylog" ]

Without variable substitution, you were checking the length of the string "keylog" which is not zero.

In addition to the bash man page, there's also the Advanced Bash-Scripting Guide and the BASH Frequently Asked Questions as good sources of information and examples.
 
Old 07-29-2007, 12:26 AM   #4
fanbin
LQ Newbie
 
Registered: Jul 2007
Distribution: fedora 6
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for replying, this is my code.

Code:
read keylog
if [ -z $keylog ]; then
data=1
else 
data=0
fi
What I really want is to ask the programme to check whether I press the enter key, if so, data=1, otherwise data=0, is my code right?
 
Old 07-29-2007, 12:35 AM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Yes, but your variable usage is not intuitive. That is, if data=1 there is no data (just enter) and if data=0 there is data. Very confusing to someone else that would have to read your code (that'll be you in a few weeks ).

Last edited by macemoneta; 07-29-2007 at 12:37 AM.
 
Old 07-29-2007, 12:54 AM   #6
fanbin
LQ Newbie
 
Registered: Jul 2007
Distribution: fedora 6
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks, but I still don't get you ? no data?

by the way, the link in your qmd is broken.

Thanks again.
 
Old 07-29-2007, 01:05 AM   #7
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
When writing software, variable=1 is generally used to denote a "true" condition and variable=0 denote a false condition.

In your code when the length of the string is zero (there is no data), you set variable data=1. That is the opposite of the state most programmers would interpret.

OK, I give up. What's a qmd?

Last edited by macemoneta; 07-29-2007 at 01:07 AM.
 
Old 07-29-2007, 01:12 AM   #8
fanbin
LQ Newbie
 
Registered: Jul 2007
Distribution: fedora 6
Posts: 8

Original Poster
Rep: Reputation: 0
ah, I see, that really helps.

qmd , er I mean:

Quote:
Please read: How To Ask Questions The Smart Way. It will save you time and frustration.

qmd is a common phrase in china refering to the things above.
 
Old 07-29-2007, 09:00 AM   #9
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
The link works; it may be blocked by your ISP.
 
Old 07-29-2007, 09:08 AM   #10
fanbin
LQ Newbie
 
Registered: Jul 2007
Distribution: fedora 6
Posts: 8

Original Poster
Rep: Reputation: 0
Really thanks. Have a nice day.
 
  


Reply



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
New guy hello lincare LinuxQuestions.org Member Intro 4 06-26-2006 11:26 PM
another guy with another Fedora Core 4 problem pingu_penguin Linux - Software 3 09-15-2005 01:16 AM
Not ANOTHER NEW GUY!!!!!! patfogle LinuxQuestions.org Member Intro 3 01-22-2005 09:52 AM
New guy! cdc7 LinuxQuestions.org Member Intro 2 01-10-2005 09:22 PM
new guy... Pongo Linux - Newbie 4 11-17-2002 09:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 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
Open Source Consulting | Domain Registration