X-Spam-Check-By: sourceware.org
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: text/plain; 	charset="us-ascii"
Subject: Problem when using variable assignment, backticks in shell script
Date: Mon, 28 Aug 2006 16:43:03 -0400
Message-ID: <017630AA6DF2DF4EBC1DD4454F8EE29708D75075@rsana-ex-hq1.NA.RSA.NET>
From: "Silva, Russell" <rsilva@rsasecurity.com>
To: <cygwin@cygwin.com>
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id k7SKh7gP003604

I am having a problem using Cygwin, variable assignment, and backticks
when shell scripting.  Occasionally, variables assigned using a
backticked expression are not properly assigned; they are left empty.
The problem appears to be non-deterministic.

For instance, take this script:

<bug_reproduce.sh>
#!/bin/bash

# create a file called "temp" with text contents "liars"
rm temp
touch temp
echo liars > temp

# make 1000 attempts to reproduce the bug
for i in `seq 1 1000`
do
  # use a backticked expression to assign the contents of "temp" 
  #   to variable $x
  x=`/usr/bin/cat < temp`;
  if [[ $x != "liars" ]]
  then
    # if this code is reached, the variable assignment failed somehow
    echo error attempt $i: \"$x\" is not the correct string \"liars\"
  fi
done

# cleanup
rm temp
</bug_reproduce.sh>

A few times out of the 1000 possible, the error code is reached:

$ ./bug_reproduce.sh
error attempt 245: "" is not the correct string "liars"
error attempt 261: "" is not the correct string "liars"
error attempt 800: "" is not the correct string "liars"

The bug is not deterministic from execution to execution:

$ ./bug_reproduce.sh
error attempt 230: "" is not the correct string "liars"
error attempt 322: "" is not the correct string "liars"
error attempt 530: "" is not the correct string "liars"

Sometimes no errors occur at all, and there is nothing special about 3
errors -- there are sometimes 1 error, say, or 4.

The problem is not unique to the "cat" program in the backticked
expression.  The "wc" program will reveal the error as well, as in this
script:

<bug_reproduce2.sh>
#!/bin/bash
rm temp
touch temp
for i in `seq 1 100`
do
  echo liars >> temp
done

for i in `seq 1 1000`
do
  x=`/usr/bin/wc -l < temp`;
  if [[ $x -ne 100 ]]
  then
    echo error attempt $i: \"$x\" not equal to 100
  fi
done
</bug_reproduce2.sh>

"wc -l" reports the number of lines in a file; in this case the line
count should always be 100.  But again, the error crops up a few times
out of a thousand:

$ ./bug_reproduce2.sh
error attempt 119: "" not equal to 100
error attempt 875: "" not equal to 100

Regards,

Russell Silva

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


