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="" ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
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
}
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}
Code:
config_replace somemachine ${SRC_DIR}/some.conf ${TARGET_DIR}/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 -- Best Regards, Daniel |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |