Possibly the best shell programming mistake ever

A colleague, lets call him Lewis, just popped over with the most bizarrely behaving shell script I have seen.

The problem was that the script would hang while the automounter timed out an attempt to NFS mount a file system on the customer's system.

I narrowed it down to something in a shell function that looked like this:

# Make a copy even if the destination already exists.
safe_copy()
{
 	typeset src="$1"
	typeset dst="$2"

	/\* Nothing to copy \*/
	if [ ! -f $src ] ; then
		return
	fi

        if [ ! -h $src -a ! -h $dst -a ! -d $dst ] ; then
		cp -p $src $dst || exit 1
	fi
}

safe_copy was called with a file as the $1 and a file as $2.

I laughed when saw the problem. Funny how you can read something and miss such an obvious mistake!

Thankfully the script has quietly been fixed.

Comments:

"Lewis" has been writing too much C I guess. :-)

Posted by Brett Monroe on June 05, 2009 at 12:45 PM BST #

I should point out that "Lewis" did not write the script. Perhaps he's been reading to much C or even D!

Posted by Chris Gerhard on June 06, 2009 at 04:37 AM BST #

As someone that does a lot of C but not much shell, any hints for the rest of us that haven't figured it out yet?

Posted by Sam Shinozaki on June 08, 2009 at 09:57 AM BST #

@Sam.

The problem is the comment.

/\* does not delimit comments in the shell. Instead thanks to globbing it expands to every file in the root directory.

Posted by Chris Gerhard on June 08, 2009 at 11:08 AM BST #

Post a Comment:
Comments are closed for this entry.
About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today