[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-bash] Overwriting of data in a file
From: |
Bernard Fay |
Subject: |
[Help-bash] Overwriting of data in a file |
Date: |
Mon, 4 Jun 2018 07:38:20 -0400 |
Hello,
In the following exerpt of a script I am writing, I try to add the lines
found in the associative array PERMVAL or simply overwrite the equivalent
entry in a file identified by PERMFILE to make sure it has the appropriate
values. The goal is to have this script executed once every few days or
week to make sure no one would have try to modify the file PERMFILE.
The problem is that an entry like "/root" will overwrite an exiting entry
or "/root/.cshrc" therefore creating a double entry for "/root" and adding
a new entry for "/root/.cshrc".
For example, let say we have the following entries in permissions.local
(PERMFILE):
pgiststinf02:~/scripts # tail permissions.local
#/usr/bin/Xorg root:root 4711
/root root:root 1234
/root/.cshrc root:root 2345
/var/log/sa root:root 3456
We run the script:
pgiststinf02:~/scripts # ./permsFile.exerpt
The file permissions.local will now look like this:
pgiststinf02:~/scripts # tail permissions.local
#/usr/bin/Xorg root:root 4711
/root root:root 0700
/root root:root 0700
/var/log/sa root:root 0770
/root/.cshrc root:root 0400
Does someone would have an idea of how to avoid this duplication of entry?
Both arrays, ELMT and PERMVAL, are actually much bigger than 3 values.
#!/bin/bash -x
declare -a ELMT=(
/root
/root/.cshrc
/var/log/sa
)
declare -A PERMVAL=(
[/root]="/root root:root 0700"
[/root/.cshrc]="/root/.cshrc root:root 0400"
[/var/log/sa]="/var/log/sa root:root 0770"
)
PERMFILE=permissions.local
for VAL in ${ELMT[*]}; do
if grep -q -F "${VAL}" ${PERMFILE}; then
echo sed -i "s|.*${VAL}[[:blank:]]*.*|${PERMVAL[${VAL}]}|"
${PERMFILE}
sed -i "s|.*${VAL}[[:blank:]]*.*|${PERMVAL[${VAL}]}|" ${PERMFILE}
else
printf "${PERMVAL[${VAL}]}\n" >>${PERMFILE}
fi
done
#chkstat --set permissions.local
Thanks,
Bernard
- [Help-bash] Overwriting of data in a file,
Bernard Fay <=