All pastes #359300 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-02-16 18:39 UTC
rendered paste body
#!/usr/bin/ksh
#
# $Id: fixpath.sh,v 1.1.1.1 2006/06/19 17:49:28 10057123 Exp $
#
# fix paths in SyncML files to point to local server instead of distributed
# server
#
# Ben Secrest <blsecres@ssdtgsm.com>
#


set -- $(getopt b:g:sx: ${*})

SAVEBAK=0

for i in ${*}
do
	case ${i} in
	-b)	BADURL=${2}; shift 2;;
	-g)	GOODURL=${2}; shift 2;;
	-s)	SAVEBAK=1; shift;;
	-x)	FILETAG=${2}; shift 2;;
	--)	shift; break;;
	esac
done

if [ ${?} -ne 0 -o "${BADURL}" == "" -o "${GOODURL}" == "" \
		-o "${FILETAG}" == "" ]
then
	echo usage: ${0} [-s] -b string -g string -x string
	echo	-b Bad URL to replace
	echo	-g Good URL to insert
	echo	-s Save backups [Default is to delete]
	echo	-x Filename tag to search for strings in
	echo
	echo ex: ${0} -b localhost/cgi-bin -g waptest.ssdtgsm.com/replay -x xml
	exit
fi

echo Attempting to replace all occurences of ${BADURL}
echo with ${GOODURL} in files:

# find and process all xml source files
for f in $(${FIND} . -name "*.${FILETAG}")
do
	# see if the file contains the bad URL
	if ${GREP} -q ${BADURL} ${f}
	then
		# if so, make a backup, remove dos line feeds and use sed
		# to replace the bad with the good
		${CP} ${f} ${f}.bak
		${CAT} ${f}.bak | ${TR} -d '\r' \
			| ${SED} "s,${BADURL},${GOODURL}," > ${f}

		if [ ${SAVEBAK} -eq 0 ]
		then
			${RM} ${f}.bak
		fi
		echo ${f}
	fi
done