LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 11-03-2007, 08:58 AM   #1
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 441

Rep: Reputation: 30
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?
 
Old 11-03-2007, 09:06 AM   #2
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 441

Original Poster
Rep: Reputation: 30
Ooops I need "quotation marks."

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

Last edited by binarybob0001; 11-03-2007 at 09:20 AM.
 
Old 11-03-2007, 09:52 AM   #3
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by binarybob0001 View Post
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.
 
Old 11-03-2007, 10:28 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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

Code:
[ "$string1" = "$string2" ]
 
Old 11-03-2007, 10:51 AM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by binarybob0001 View Post
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.
 
Old 11-03-2007, 11:54 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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"
 
  


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
Infinite Loop ewt3y Programming 3 08-16-2005 09:48 AM
Java Stopping an Infinite loop oulevon Programming 3 10-18-2004 10:11 PM
infinite loop beginner_84 Programming 5 08-15-2004 02:32 AM
fetchmail stuck in infinite loop Prommy Linux - Software 0 02-17-2004 08:15 AM
Sendmail build - infinite loop?? kstarks Linux - Software 0 10-02-2003 07:13 AM


All times are GMT -5. The time now is 11:13 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration