Find the answer to your Linux question:
Results 1 to 5 of 5
I found this script on the ubuntu forum, it does work but it seems to me that if the internet connection is good it makes recursive calls to the subroutine. ...
  1. #1
    Just Joined! spg666's Avatar
    Join Date
    Dec 2008
    Location
    UK
    Posts
    24

    Bash script, will this cause a stack overflow?

    I found this script on the ubuntu forum, it does work but it seems to me that if the internet connection is good it makes recursive calls to the subroutine. This would eventually result in a stack overflow in most environments, it this true with a bash script? or am I way off the mark?

    Code:
    #! /bin/bash
    
    main()
    {
        if ping -c 1 64.233.169.103 > /dev/null;then
            sleep 30
            main
        else
            zenity --info --text "Internet Lost";
        fi
    }
    
    main

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So interestingly, this is actually a special case of recursion called tail recursion. A tail recursive function makes its very last action a recursive call. Because nothing happens after the recursive call, the recursion can actually be optimized, and a tail recursive function can recur infinitely without growing stack space. Strictly functional languages like Lisp or Haskell rely on this optimization in order to work.

    Having said that, I strongly suspect that Bash does not perform tail call optimization. In this case, I would think that eventually, yes, you would use all of your memory. However, at 30 seconds between stack frames, and with no data lying around, the script can probably go for a LONG time before you would have any problem. Even then, most of the stack would be swapped out, so you wouldn't have any problem with your memory being full.

    But yes, a loop would be a better approach in this instance.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    I suppose the answer is true if the following holds:

    1] there is no compiler and hence no code optimizer for scripts that rewrites recursive calls without returns as loops / jumps
    2] the bash interpreter invokes the recursive call without keeping a handle to return to (i.e. overrides the current stack entry) because it knows that there is no return value (because there is no "return" statement)

    Anyway, I am quite sure that these kind of optimizations were not built in and you could find it out easily by removing the sleep.

    Nontheless I would restructure that thing a little:

    Code:
    #!/bin/bash
    # expects $1 to be a ip or hostname
    check()
    {
         return ping -c 1 $1 > /dev/null
    }
    
    main()
    {
        while check(64.233.169.103); do
            sleep 30
        done
        zenity --info --text "Internet Lost";
    }
    
    main
    And still it remains semantically the same, so a real advantage isn't in there.

  4. #4
    Just Joined! spg666's Avatar
    Join Date
    Dec 2008
    Location
    UK
    Posts
    24
    That what I suspected, it looked like bad programming practice (to me). I started programming in a time when memory was a scares resource and you programmed in assembler, it shape the way you code.

  5. #5
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    Yes, I have always in mind that code should be easily understandable, maintainable and should not depend on compiler optimizations or other environment influences. So recursive calls are neat, but should be used only if they have a PRO and no CON because usually one day a person that may know/understand less than you will become mentally overwhelmed because it has to deal/maintain your dark magic code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...