delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/08/28/16:43:10

X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
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 AT rsasecurity DOT com>
To: <cygwin AT cygwin DOT com>
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
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/


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019