This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Problem when using variable assignment, backticks in shell script


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]