Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Shell Script Bug/Puzzle 3

Status
Not open for further replies.

SamBones

Programmer
Aug 8, 2002
3,186
US
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).


 
man ksh
use another variable name instead of SECONDS

HTH,

p5wizard
 
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:
 
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.
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.
 
I believe if you change ITERATIONS to something else that will solve the behavior
 
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..
 
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..
 
Here's a big hint. Just log in and type this...
Code:
echo $SECONDS
echo $SECONDS
echo $SECONDS

ITERATIONS is fine.



 
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.
 
I mostly use FunnyCapitalizedNames for my shell variables to avoid such weird behaviour...

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top