LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell infinite loop hell (https://www.linuxquestions.org/questions/programming-9/shell-infinite-loop-hell-596841/)

binarybob0001 11-03-2007 08:58 AM

Shell infinite loop hell
 
Code:

ExtractDir()
{
  i=1
  dir=""
  tmp=" "              #`echo $1 | cut -f $i -d '/' `
  term=" "
  while [ $tmp != $term ]
  do
    echo "Man this sucks!!"
  done
  return
}

find . -type l -exec ls -l {} \; | while read var
do
  pertanent=`echo $var | cut -f 8- -d ' ' | cut -b3-`
  link=`echo $pertanent | awk -F" -> " ' { print $1 }' `
  targ=`echo $pertanent | awk -F" -> " ' { print $2 }' `
  echo $link
  ExtractDir $link
done

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?

binarybob0001 11-03-2007 09:06 AM

Ooops I need "quotation marks."

Nope, that wasn't the answer. What is the answer?

bartonski 11-03-2007 09:52 AM

Quote:

Originally Posted by binarybob0001 (Post 2946785)
Code:


  tmp=" "              #`echo $1 | cut -f $i -d '/' `
  term=" "
  while [ $tmp != $term ]


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.

matthewg42 11-03-2007 10:28 AM

It's a good idea when doing string comparisons to quote the strings in question, i.e.

Code:

[ "$string1" = "$string2" ]

David1357 11-03-2007 10:51 AM

Quote:

Originally Posted by binarybob0001 (Post 2946785)
Code:

  while [ $tmp != $term ]

The easiest way to solve this problem in a bullet-proof manner is to use the following syntax:

Code:

  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.

ghostdog74 11-03-2007 11:54 AM

i am more curious about what you want to do instead.

besides that, you can cut down on the cuts...and use -ls instead of -exec ls
Code:

find /path -type l -ls  | awk '{print $(NF-3),$NF,$(NF-2)}
another way using GNU find
Code:

find /path -type l -printf "%AH:%AM %p %l"


All times are GMT -5. The time now is 06:29 PM.