Skip to content

Possibly the best shell programming mistake ever

June 5, 2009

 

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.

 

Advertisements

From → Solaris

4 Comments
  1. Brett Monroe permalink

    "Lewis" has been writing too much C I guess. 🙂

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

  3. Sam Shinozaki permalink

    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?

  4. @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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: