nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Weird variable evaluation problem (FreeBSD, sh)

This is a discussion on Weird variable evaluation problem (FreeBSD, sh) within the Shell scripting forums, part of the Development/Scripting category; Hi! I have a function in sh (FreeBSD) which looks like this: Code: config_replace() { SOURCE=$2 TARGET=$3 VARS_FILE=M_Config_vars_only_$1.inc SED_CMD_O="" CMD_00="" ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 10-06-2006, 06:44 PM
Junior Member
 
Join Date: Oct 2006
Posts: 1
Rep Power: 0
fangorn
Default Weird variable evaluation problem (FreeBSD, sh)

Hi!

I have a function in sh (FreeBSD) which looks like this:

Code:
config_replace() {
        SOURCE=$2
        TARGET=$3
        VARS_FILE=M_Config_vars_only_$1.inc
SED_CMD_O=""
CMD_00=""
exec 3<> ${VARS_FILE}
while read line <&3
do {
if [ ! `echo $line |grep "^#"` ]
then
MY_VAR=`echo $line|awk -F"=" '{print $1}'`
MY_VAL=`echo $line|awk -F"=" '{print $2}'`
if [ `grep -so -m 1 "__${MY_VAR}__" $SOURCE` ]
then
SED_CMD_0=$(eval echo -n ${SED_CMD_0} -e 's,__${MY_VAR}__,${MY_VAL},g')
fi
fi
}
done
echo "${SED_CMD_0}">/tmp/tmp.cmd
exec 3>&-
ble=$(sed -e "s,-e\ s,-e\ \'s,g" -e "s,\,g,\,g\',g" /tmp/tmp.cmd)
eval sed $ble $SOURCE > $TARGET
}
is fed with a config file like this:
Code:
M_HOST_SHORT=${M_NAME}
M_DOMAIN="somedomain.com"
M_HOST_LONG=${M_HOST_SHORT}.${M_DOMAIN}
M_SRV_NO="13"
M_APP_NET_PFX="192.168.100"
M_NFS_NET_PFX="192.168.101"
M_NFS_IP=${M_NFS_NET_PFX}.${M_SRV_NO}
M_APP_IP=${M_APP_NET_PFX}.${M_SRV_NO}
and so on. I call it like this:

Code:
config_replace somemachine ${SRC_DIR}/some.conf ${TARGET_DIR}/some.conf
and naturally it replaces all variables like __M_APP_IP__ it finds in some.conf

The function works fine mostly, there's but one problem: the last variable in the config file doesn't get "evaluated" (for the lack of a better word) ONLY if it's a complex variable. To clarify things:

If the last variable found in some.conf is M_HOST_SHORT, or anything with one value, or even one variable - works fine.

If on the other hand it's M_APP_IP - no values will be placed where they shoud, just the "${M_APP_NET_PFX}.${M_SRV_NO}".

And this only happens for the last variable so I assume there's something wrong with the loop. Can't find what, so maybe you could help me understand my own code . Thanks in advance.

--
Best Regards,
Daniel
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-10-2006, 03:37 AM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
My distro: Suse, RHEL, Vista
Posts: 154
Rep Power: 4
sweta will become famous soon enough
Default

Wow that script is really badly written. All I can say is run script as follows to see what is going on:
Code:
sh -x config_replace somemachine ${SRC_DIR}/some.conf ${TARGET_DIR}/some.conf
or better pass -v option too

Code:
sh x- v- config_replace somemachine ${SRC_DIR}/some.conf ${TARGET_DIR}/some.conf
Above two statements will give you lots of debugging info.
__________________
Friends - v-nessa - missyAdmin - LinuxChix
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
environmental variable myfoot CentOS / RHEL / Fedora 1 02-01-2008 07:14 AM
How to pass shell variable to awk program Shell scripting 4 06-23-2006 06:11 AM
Weird Debian login problem jerry Linux software 2 06-29-2005 12:08 AM
HTML variable nathan86 Linux software 3 03-23-2005 01:29 PM
FreeBSD putty home key problem chiku All about FreeBSD/OpenBSD/NetBSD 2 02-03-2005 06:10 PM


All times are GMT +5.5. The time now is 07:37 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36