ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
At one point I had this working then it stopped working now it refuses to work at all. How is it possible that the while loop at the top is infinite? If I change the sign to equals it is still infinite. One would think two strings are either equal or not equal, but the shell thinks they can be both at the same time. What happened to logic?
The problem is that you are using a space character as your variable. The space character is one of the default word delimiters in bash (i.e. one of the characters in $IFS), therefore your while loop was actually doing this:
while [ != ]
And not seeing anything to compare true or false. This is why it didn't matter whether you were comparing using != or = ... it wasn't actually doing a valid comparison.
If you change the values of $tmp and $term to, say, 'X', your loop will terminate.
The easiest way to solve this problem in a bullet-proof manner is to use the following syntax:
while [ "x$tmp" != "x$term" ]
If either $tmp or $term evaluate to the empty string, you will prevent problems by sticking that extra "x" in there. You can actually use any character, and you can even put the character outside the quotes, so this is just one way of handling it.
I forget all the rules for how bash decides what is an empty string because I just use the syntax above, and it always works.