2 #*******************************************************************************
3 # ============LICENSE_START=======================================================
5 # ================================================================================
6 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
20 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 #*******************************************************************************
23 # Copyright 2011 AT&T Intellectual Properties
24 ##############################################################################
25 # findreplace.sh <template> <destination>
27 # This script searches a provided file for templatized variable names
28 # in the format __varname__ and, if found in the current environment
29 # replaces those. Once complete, it will move the final copy of the file
32 ##############################################################################
33 TEMPLATE=${1:?"Template file path required"}
34 DESTINATION=${2:?"Destination file path required"}
36 if [ ! -f "${TEMPLATE}" ]; then
37 echo "ERROR: Specified template file does not exist: ${TEMPLATE}"
41 DIRECTORY=`dirname ${DESTINATION}`
42 if [ ! -d "${DIRECTORY}" ]; then
43 echo "ERROR: Destination directory does not exist: ${DIRECTORY}"
50 # create a sed script for replacing variables from current environment
51 for i in `env | awk -F= '{ print $1}'`; do
52 if [ "$i" = "IFS" ] ; then
56 VALUE=`eval echo '$'${i}` || {
57 echo 'WARNING: Unable to format '${i}' for sed replacement'
61 for x in '@' '^' '&' '?' '#' '~' '%' '|' '+' '/'; do
62 echo ${VALUE} | grep "$x" 2>/dev/null 1>/dev/null
69 if [ -z "${CCHAR}" ]; then
70 echo "WARNING: Unable to find a suitable sed replacement character for ${VALUE}, will ignore setting ${KEY} in templates"
74 echo " s${CCHAR}__${i}__${CCHAR}${VALUE}${CCHAR}g" >> ${SED_SCR}
77 sed -e 's/\\\@/\\\\@/g' ${SED_SCR} > ${SED_SCR}.1 || exit 300
79 if [ -f ${DESTINATION} ]; then
80 TIMESTAMP=`date +%Y%m%d%H%M%S`
81 o_dir=`dirname ${DESTINATION}`
82 o_file=`basename ${DESTINATION}`
83 mv ${DESTINATION} ${o_dir}/bu.${o_file}.${TIMESTAMP}
86 mv -f ${SED_SCR}.1 ${SED_SCR} || exit 400
88 echo "}" >> ${SED_SCR} || exit 500
90 sed -f ${SED_SCR} ${TEMPLATE} > ${DESTINATION} || exit 600