I am trying to change the variable reload from 0 to 1 as you can see in my code below:
for d in /home/*; do
u="${d##*/}";
reload=0;
echo "Checking $u";
if [ -e /home/$u/info/reload.php ]; then
echo "FOUND!";
reload=1;
fi
done
echo $reload;
if [ $reload = 1 ]; then
service apache2 reload
fi
The problem is that it doesn’t get changed, $reload remains as 0, the output is similar to this (apache does not get reloaded):
Checking user1
FOUND!
Checking user2
0 #< this should be 1 not 0 :(
Why won’t my bash variable change??
You’re resetting
reloadfor each user. If the file is not found for the last user,reloadwill be0when you exit the loop.Suggested fixes (with indentation for readability):