Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...If I'd only had resource like eng-tips when I was just getting started! I might have dazzled them with my brilliance instead of my BS..."

Geography

Where in the world do Tek-Tips members come from?

Weird Shell Script Bug/PuzzleHelpful Member!(3) 

SamBones (Programmer)
29 Jul 10 21:16
OK, I have a little bit of Korn shell script that is exhibiting some VERY weird behavior. I figured out what was happening, so I don't need someone to tell me what's going on. But, I thought it was kind of funny so I thought I'd share it as a little programming puzzle! Here it is.

I have a little test script that loops a number of times. In each loop, it sleeps for several seconds just to slow down the loop. Here's the code...

CODE

#!/bin/ksh

SECONDS=2
ITERATIONS=5

while (( ITERATIONS ))
do
        print "Waiting for ${SECONDS} seconds"
        sleep ${SECONDS}

        (( ITERATIONS -= 1 ))
done

print "Done!"
The expected output is that it will print out "Waiting for 2 seconds", five times, with a delay of two seconds after each, then print "Done!" and exit.

The actual output is very different...

CODE

Waiting for 2 seconds
Waiting for 4 seconds
Waiting for 8 seconds
Waiting for 16 seconds
Waiting for 32 seconds
Done!
There is nothing in the script that increments the variable SECONDS, but it keeps doubling each time! It takes a total time of 62 seconds (1 minute 2 seconds) to run.

Do you know why?

A hearty attaboy/attagirl (plus a star) to anyone that answers it without peeking at other peoples' answers (use the spoiler tag to hide your answer).


 
Helpful Member!  p5wizard (IS/IT--Management)
29 Jul 10 21:51

Spoiler:

man ksh
use another variable name instead of SECONDS

HTH,

p5wizard

Helpful Member!  stefanwagner (Programmer)
29 Jul 10 22:12

Spoiler:


man ksh:
    SECONDS
      Each time this variable is referenced, the number of seconds since shell invocation is returned. If this variable is assigned a value, then the value returned upon reference will be the value that was assigned plus the number of seconds since the assignment.

don't visit my homepage: http://home.arcor.de/hirnstrom/minis/index.html

Helpful Member!  SamBones (Programmer)
30 Jul 10 12:13
Heh, heh, yeah. bigsmile

I'll try not to give anything away with this comment, but when you're writing a script quickly, you chose names that make sense, and are obvious as to what their purpose or use is. That name seemed like a good choice. This "problem" was actually coded into a much larger script that ran for days, and was exhibiting some very weird symptoms as you can imagine.

Spoiler:

I also had a coworker try to use the variable name "PATH" for something other than PATH. He was really confused when almost everything in his script stopped working.
jmanj (Programmer)
30 Jul 10 17:28
I believe if you change ITERATIONS to something else that will solve the behavior
jmanj (Programmer)
30 Jul 10 17:51
I also think that SECONDS is a reserved word and if there is a value in it then that value is multiplied by that value for every encounter or iteration. It seems to me that way but I maybe wrong..
jmanj (Programmer)
30 Jul 10 17:57
Okay I'm close but no cigar!! But here's a better explanation:

If a value is assigned to SECONDS, the sum of that value and the number of seconds since its assignment is displayed when the variable is referenced:

Courtesy of the INTERNET..
SamBones (Programmer)
2 Aug 10 12:53
Here's a big hint. Just log in and type this...

CODE

echo $SECONDS
echo $SECONDS
echo $SECONDS

ITERATIONS is fine.



 
Annihilannic (MIS)
2 Aug 10 21:24
I guess that's why when Larry wrote Perl he choose built-in variable names that no-one else would be crazy enough to use.  $_, $`, $<, $', etc...

Annihilannic.

p5wizard (IS/IT--Management)
3 Aug 10 3:21
I mostly use FunnyCapitalizedNames for my shell variables to avoid such weird behaviour...

HTH,

p5wizard

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close