JuangaCovas.info

La página personal de Juan Gabriel Covas

Herramientas de usuario

Herramientas del sitio


personal:codigo:bash-change-conf

Bash: función change_conf

–Juan Gabriel Covas 2015

Busca “key=value” en el archivo especificado y añade la línea si no existe o sustituye el valor si ya existe

Función change_conf para BASH

#######################################################################################################################
# Custom Function to search/replace/add a "key=value" pair on a given .conf file.
#   Supports line commented with #key...
#   Supports key=value in form of "key = value" which is good for sysctl.conf, etc. since the search is done for
#   "key" and then replaced/added

change_conf() {

	if [[ $# -ne 3 ]]; then
		echo "ERROR: change_conf function. Expected parameters: 3"
		echo "Example: change_conf /etc/file.conf key value"
		exit 1
	else
		sr_file="$1"
		basename_sr_file=$(basename $1)
		sr_key="$2"
		sr_value="$3"
		replacement="${sr_key}=${sr_value}"

		# for i in "$@"; do echo $i ; done

		# create the file if not found
		if [ ! -f "$sr_file" ] ;then 
			echo "Created new file: ${sr_file}";
			touch ${sr_file};
			printf '%s' ${sr_file}
		else

			# start by printing the file to be changed
			printf '%s' ${sr_file}

			bCommented=0
			grep -q -E "^#${sr_key}\s=" ${sr_file} && bCommented=1;
			if [ $bCommented -eq 0 ] ;then
				grep -q -E "^#${sr_key}=" ${sr_file} && bCommented=1;
			fi
			if [ $bCommented -eq 1 ] ;then
				sed -i "s/#${sr_key} =/${sr_key} =/w /tmp/mychglog.txt" ${sr_file}
				if [ ! -s /tmp/mychglog.txt ] ;then
					sed -i "s/#${sr_key}=/${sr_key}=/w /tmp/mychglog.txt" ${sr_file}
				fi
				
				printf " ---> Removed # "
			fi
		fi
		
		bFound=0
		cur_value=""
		grep -q -E "^${sr_key}=" ${sr_file} && bFound=1; cur_value=$(grep -E "^${sr_key}=" ${sr_file})
		if [ $bFound -eq 0 ] ;then
			grep -q -E "^${sr_key} =" ${sr_file} && bFound=2; cur_value=$(grep -E "^${sr_key} =" ${sr_file})
			search_str="^${sr_key} =";
		else
			search_str="^${sr_key}=";
		fi

		# tell what will be done for this key/value pair
		if [ ! -z "${cur_value}" ] ;then
			IFS='=' read -ra value_parts <<< "$cur_value"

			# we will try to change or add, only if value is different
			# printf ' - Testing [%s] Vs. [%s] - ' "${replacement}" "${cur_value}"
			if [[ "${replacement}" == "${cur_value}" ]]; then
				printf ' ------> SKIP [%s] [%s]\n' "${sr_key}" "${value_parts[1]}"
				return 0
			fi
			printf ' ---> REPLACE [%s] to [%s] ... ' "${cur_value}" "${replacement}"
		else
			printf ' -------> ADD [%s] [%s] ... ' "${sr_key}" "${sr_value}"
		fi

		# do the search/replace/add
		sudo awk -v replacement="${replacement}" \
		         -v search="${search_str}" \
		         'match($0, search) {print replacement; found=1} !match($0, search) {print $0} END {if (!found) {print replacement }}' \
		         ${sr_file} > /tmp/tmp-${basename_sr_file}

		# cat /tmp/tmp-${sr_file}
		cat /tmp/tmp-${basename_sr_file} | sudo tee ${sr_file} >/dev/null

		# check the resulting file
		grep -q -e "^${replacement}" $sr_file
		RETVAL=$?
		[ $RETVAL -eq 0 ] && printf 'OK.\n'; return 0
		[ $RETVAL -ne 0 ] &&  printf 'FAILED!\n'; return 1
	fi
}

personal/codigo/bash-change-conf.txt · Última modificación: 10/07/2020 17:50 por Juanga Covas