ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.