diff options
author | B Stack <bgstack15@gmail.com> | 2016-10-20 16:15:23 -0400 |
---|---|---|
committer | B Stack <bgstack15@gmail.com> | 2016-10-20 16:15:23 -0400 |
commit | d60ed67fa4eeeba312646358316e49c0a6cbc6f6 (patch) | |
tree | 7cec285c82c1e15cc021086899165afb7bed2385 /updateval.sh | |
download | deployscripts-d60ed67fa4eeeba312646358316e49c0a6cbc6f6.tar.gz deployscripts-d60ed67fa4eeeba312646358316e49c0a6cbc6f6.tar.bz2 deployscripts-d60ed67fa4eeeba312646358316e49c0a6cbc6f6.zip |
initial commit
Diffstat (limited to 'updateval.sh')
-rw-r--r-- | updateval.sh | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/updateval.sh b/updateval.sh new file mode 100644 index 0000000..08f431a --- /dev/null +++ b/updateval.sh @@ -0,0 +1,63 @@ +#!/bin/sh +# File: /root/updateval.sh +# Package: deployscripts +# Author: bgstack15 +# Startdate: 2016-07-27 +# Title: Script that Updates/Adds Value +# Purpose: Supposed to allow idempotent and programmatic modifications to config files +# History: 2016-08-01 last modified main content +# 2016-10-11 Replaced in bgscripts with python3 script. The shell version is maintained for the deployscripts package. +# Usage: ./updateval.sh /etc/rc.conf "^ntpd_enable=.*" 'ntpd_enable="YES"' --apply +# Reference: +# "Building the FreeBSD 10.3 Template.docx" +# Improve: +# Document: Below this line + +infile="${1}" +searchstring="${2}" +destinationstring="${3}" +doapply="${4}" +tmpfile="$( mktemp )" +lineexists=0 + +#determine sed command +case "$( uname -s )" in + FreeBSD) sedcommand=gsed; formatstring="-f %p";; + Linux|*) sedcommand=sed; formatstring="-c %a";; +esac + +#linenum=$( grep -niE "${searchstring}" "${infile}" | awk -F: '{print $1;}' ) +linenum=$( awk "/${searchstring}/ { print FNR; }" "${infile}" ) +#echo "linenum=\"${linenum}\"" +for word in ${linenum}; +do + #echo "word=${word}" + if test -n "${word}" && test ${word} -ge 0; + then + # line number is valid + lineexists=1 + #echo "##### line number is valid" + if test "${doapply}" = "--apply"; + then + #echo $sedcommand -i -e "s/${searchstring}/${destinationstring}/;" ${infile} + $sedcommand -i -e "s/${searchstring}/${destinationstring}/;" ${infile} + else + #echo $sedcommand -e "s/${searchstring}/${destinationstring}/;" ${infile} + $sedcommand -e "s/${searchstring}/${destinationstring}/;" ${infile} + fi + fi +done +if test "${lineexists}x" = "0x"; +then + # must add the value + #echo "##### must add the value" + if test "${doapply}" = "--apply"; + then + { cat "${infile}"; printf "${destinationstring}\n"; } > ${tmpfile} + _perms=$( stat ${formatstring} "${infile}" | tail -c5 ) + mv "${tmpfile}" "${infile}" + chmod "${_perms}" "${infile}" + else + { cat "${infile}"; printf "${destinationstring}\n"; } + fi +fi |