0

I have this code to check/change umask values using two loops inside ( for.. and while..) Only issue is that I can't monitor if UMASK was changed via variable umaskOK, because inside loop passing values not working. ANy idea how to do it ? In my case var umaskOK outside of the loop still shows 0, even I changed the value to 1 inside loop.


UMASK_FILES=("/root/.bash_profile" "/root/.bashrc" "/root/.cshrc" "/root/.tcshrc" "/etc/profile" "/etc/bashrc" "/etc/csh.cshrc")
ERROR_LOG="error.log"
UMASK_ATTR="077"
umaskOK=0
echo "checking Variable UMASK_OK before loop == $umaskOK"
echo

for i in ${UMASK_FILES[@]}; do

if [ -f "$i" ]; then

grep ^umask $i | awk ' { print $2} ' |  while read -r line1;
do
if [ "$line1"  != "$UMASK_ATTR" ]; then
umaskOK=1  # changing umaskOK value
echo "check VAR inside loop == $umaskOK"
echo "Found wrong  umask  $line1 as expected $UMASK_ATTR  in $i"
echo "Changing umask in $i"
sed -i "/^[^#]*umask/s%.*%umask $UMASK_ATTR%" $i
fi
done
fi
done

echo "Final umaskOK VAR  == $umaskOK"

output

[root@centos79 ~]# sh fix.sh
checking Variable UMASK_OK before loop == 0

check VAR inside loop == 1
Found wrong  umask  022 as expected 077  in /root/.bash_profile
Changing umask in /root/.bash_profile
check VAR inside loop == 1
Found wrong  umask  022 as expected 077  in /root/.bashrc
Changing umask in /root/.bashrc
check VAR inside loop == 1
Found wrong  umask  022 as expected 077  in /root/.cshrc
Changing umask in /root/.cshrc
check VAR inside loop == 1
Found wrong  umask  022 as expected 077  in /root/.tcshrc
Changing umask in /root/.tcshrc
Final umaskOK VAR  == 0
[root@centos79 ~]#
andrew
  • 321
  • 3
  • 14

0 Answers0