I have a script which runs differently based on current working directory. Basically it processes an .ini file and takes parameters from it and runs mysql backup.
Script is running as root user.
When I run it with working directory as /root. It exits with error. The .ini file parameters do not reflect in script.
When I do cd / then run it, it works fine.
Here is the function that seems to fail.
get_param()
{
for line in `cat ${PARAMETERSFILE}`
do
if [ "${ISFOUND}" != "true" -a "`echo ${line} | grep ${STAG} | grep -v grep | wc -l`" == "1" ]
then
ISFOUND="true"
fi
if [ "${ISFOUND}" == "true" -a "`echo ${line} | grep ${ETAG} | grep -v grep | wc -l`" == "1" ]
then
ISFOUND="false"
fi
if [ "${ISFOUND}" == "true" -a "`echo ${line} | grep ${STAG} | grep -v grep | wc -l`" != "1" ]
then
key="`echo ${line} | sed 's/=/ /' | awk '{print $1}'`"
val="`echo ${line} | sed 's/=/ /' | awk '{print $2}'`"
cmd="${key}=\"${val}\""
eval ${cmd}
fi
done
}
Some variables are as below:
STAG="\[backup\]"
ETAG="\/backup\]"
ISFOUND="false"
ini file is as below
[help]
backupdir="Backup Home Directory "
user="MySQL Database Username"
password="MySQL Database Password"
retentioPeriodMinutes="Retention Period of Backup (e.g. 1440= 1 day)"
memory="Dedicated RAM (Mbyte)"
[/help]
[backup]
backupdir=/opt/backupdb
user="root"
password="xyz"
[/backup]
[restore]
backupdir=/opt/backupdb
user="root"
password="xyz"
memory="1024"
[/restore]
[houseKeeping]
backupdir=/opt/backupdb
retentioPeriodMinutes=10080
[/houseKeeping]
Below is the output while running with sh -xv. It seems pipe is not working as it should when current directory is /root
+ for line in '`cat ${PARAMETERSFILE}`'
echo ${line} | grep ${STAG} | grep -v grep | wc -l
++ grep '\[backup\]'
++ grep -v grep
++ wc -l
+ '[' false '!=' true -a 0 == 1 ']'
echo ${line} | grep ${ETAG} | grep -v grep | wc -l
++ grep '\/backup\]'
++ echo '[help]'
++ grep -v grep
++ wc -l
+ '[' false == true -a 0 == 1 ']'
echo ${line} | grep ${STAG} | grep -v grep | wc -l
++ grep -v grep
++ grep '\[backup\]'
++ echo '[help]'
++ wc -l
Here is same output when working directory is /
+ for line in '`cat ${PARAMETERSFILE}`'
echo ${line} | grep ${STAG} | grep -v grep | wc -l
++ grep '\[backup\]'
++ grep -v grep
++ echo '[help]'
+ '[' false '!=' true -a 0 == 1 ']'
echo ${line} | grep ${ETAG} | grep -v grep | wc -l
++ grep '\/backup\]'
++ grep -v grep
++ wc -l
++ echo '[help]'
+ '[' false == true -a 0 == 1 ']'
echo ${line} | grep ${STAG} | grep -v grep | wc -l
++ echo '[help]'
++ grep -v grep
++ grep '\[backup\]'
++ wc -l
${PARAMETERSFILE}
is. I'd think that if that is relative to the current directory then that explains a lot.for line in $(cat $FILE)
part will not do what you want, the whole file will be broken up at white space andline
will contain each part in turn, i.e.[help]
,backupdir="Backup
,home
, etc.... Usewhile read line; do ... done < $FILE
grep -v grep
in your pipeline? That's something usually used when grepping the output ofps
for a command, becauseps
will also show thegrep
process. But why would you need to filter outgrep
from your init file? Just copying it without understanding it?