有关Bourne Shell语言和Bourne Again Shell语言中变量范围的问题。两者具有相同的行为。
#!/bin/sh
CHANGE_MADE="0"
printf "1\n2\n3\n " | while read NUMBER
do
if [ "$NUMBER" = "2" ]
then
CHANGE_MADE="1"
echo "In condition CHANGE_MADE= $CHANGE_MADE"
fi
echo "In loop CHANGE_MADE= $CHANGE_MADE"
done
echo "Out of loop: CHANGE_MADE= $CHANGE_MADE"
实际结果:
:~$ Public_cleaner.sh
In loop CHANGE_MADE= 0
In condition CHANGE_MADE= 1
In loop CHANGE_MADE= 1
In loop CHANGE_MADE= 1
Out of loop: CHANGE_MADE= 0
:~$
我曾期望在最后一行CHANGE_MADE为1。为什么它为零?