coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

using tee after if-fi construct clobbers variable


From: Steve Aberle
Subject: using tee after if-fi construct clobbers variable
Date: Mon, 22 Jan 2018 08:00:00 -0800
User-agent: Mozilla Thunderbird on Debian GNU/Linux 9

Tested on:
Debian 9
Raspian 9


- - - - - Test script - - - - -

#/bin/sh
# Example:      use of tee following an if-then-else-fi construct
#               clobbers a variable set within that construct.

LOG1="/tmp/test1.LOG"
LOG2="/tmp/test2.LOG"
LOG3="/tmp/test3.LOG"
echo

# redirection into a file - WORKS
VAR_1="none"
if [ "$VAR_1" == "none" ]
  then
    VAR_1="true"
    echo -e "Location 1:\t\$VAR_1 = \"$VAR_1\""
  else
    VAR_1="false"
    echo -e "Location 2:\t\$VAR_1 = \"$VAR_1\""
  fi    > $LOG1
echo -e "Location 3:\t\$VAR_1 = \"$VAR_1\"" # expected value

# tee into a file (after echo within construct) - WORKS
VAR_2="none"
if [ "$VAR_2" == "none" ]
  then
    VAR_2="true"
    echo -e "Location 4:\t\$VAR_2 = \"$VAR_2\""     | tee $LOG2
  else
    VAR_2="false"
    echo -e "Location 5:\t\$VAR_2 = \"$VAR_2\""     | tee $LOG2
  fi
echo -e "Location 6:\t\$VAR_2 = \"$VAR_2\"" # expected value

# tee into a file (after if-then-else-fi construct) - CLOBBERS VARIABLE
VAR_3="none"
if [ "$VAR_3" == "none" ]
  then
    VAR_3="true"
    echo -e "Location 7:\t\$VAR_3 = \"$VAR_3\""
  else
    VAR_3="false"
    echo -e "Location 8:\t\$VAR_3 = \"$VAR_3\""
  fi    | tee $LOG3
echo -e "Location 9:\t\$VAR_3 = \"$VAR_3\"" # clobbered value

echo -e "\nNOTE: location 9 should be the same as locations 3 and 6\n"

ls -l /tmp/test*.LOG
echo -e "\nContents of \"$LOG1\" ..."
cat $LOG1
echo -e "\nContents of \"$LOG2\" ..."
cat $LOG2
echo -e "\nContents of \"$LOG3\" ..."
cat $LOG3
echo

exit

- - - - -

Attachment: bug-test.tar.gz
Description: application/gzip


reply via email to

[Prev in Thread] Current Thread [Next in Thread]