DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   (View Single Post)  
Old 20th May 2013
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 575
Thanked 14 Times in 13 Posts
Default Unexpected / Inconsistent shell behaviour

A while ago I was working with a system shell script and encountered a really unexpected result when I tried to do something a certain way. Below I've distilled it into a simple example. Odd shell behaviour of some sort has been discussed on this forum before, but I couldn't find it, so I hope this isn't a duplicate discussion.

Consider the following example script for a Bourne-like shell:

Code:
#!/path/to/shell

VAR=Old

echo A | while read REPLY; do

    echo Here

    VAR=New

done

echo VAR = $VAR
Note that the first line of the script (in red) shouldn't be used literally. Replace it with paths to various Bourne-like shells to be tested.

At this point, to get the most out of this, you shouldn't read past the end of this paragraph. Just study the script above and decide what you think the output should be. Then come back and read the rest of the post. No cheating now, look at the script first.

Unexpected Behaviour

When I ran the script on "most" shells, the output was totally unexpected. It gave this:

Here
VAR = Old

Note that since it prints "Here", the script does go into the do loop where VAR gets set to New. Yet upon exiting the loop, VAR is back to the "Old" value! It's as if what happens in the loop is somehow quite local, and is lost after leaving the loop. I got this weird behaviour with the following shells:

bash
sh NetBSD
ksh NetBSD
ksh OpenBSD
sh SunOS

Expected Behaviour

The output I'd expected was this:

Here
VAR = New

After checking more shells, this output was produced by:

ksh93 CentOS
ksh SunOS

One conclusion is that Bourne-like shells are not consistent about this.

Question: Is the "locality" of the unexpected output intended and is it documented somewhere? I looked at some man pages a bit and didn't find anything, but maybe it's subtle or I plain missed it.

Thanks for reading, and for any comments.
Reply With Quote
 

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify rules to behaviour Droid OpenBSD Security 5 25th March 2012 05:58 PM
DragonflyBSD: unexpected pkg_admin running. colonelmooch Other BSD and UNIX/UNIX-like 0 4th August 2011 04:26 PM
Am I blind? syntax error: `(' unexpected guitarscn Programming 1 10th November 2010 08:53 PM
strange behaviour after improper shutdown karri FreeBSD General 1 15th October 2008 03:08 PM
Funny network behaviour :) PatrickBaer General software and network 5 9th October 2008 09:47 AM


All times are GMT. The time now is 01:42 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick