mardi 3 mars 2015

Shell script runs diffrently based on working directory.


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


Aucun commentaire:

Enregistrer un commentaire