From 34e59ae03075e267e8e9090dde039aefc5a0916b Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 25 Feb 2018 14:51:29 +0000 Subject: [PATCH] Tidying config utils --- src/freedombone-utils-config | 112 +++++++++++++++++------------------ 1 file changed, 54 insertions(+), 58 deletions(-) diff --git a/src/freedombone-utils-config b/src/freedombone-utils-config index 6851216a..00191c46 100755 --- a/src/freedombone-utils-config +++ b/src/freedombone-utils-config @@ -106,10 +106,10 @@ function get_completion_param { param_name="$1" if [ ${#param_name} -gt 0 ]; then - if [ $COMPLETION_FILE ]; then - if [ -f $COMPLETION_FILE ]; then - if grep -q "${param_name}:" $COMPLETION_FILE; then - param_value=$(cat $COMPLETION_FILE | grep "${param_name}:" | head -n 1 | awk -F ':' '{print $2}') + if [ "$COMPLETION_FILE" ]; then + if [ -f "$COMPLETION_FILE" ]; then + if grep -q "${param_name}:" "$COMPLETION_FILE"; then + param_value=$(grep "${param_name}:" "$COMPLETION_FILE" | head -n 1 | awk -F ':' '{print $2}') echo "$param_value" return fi @@ -123,7 +123,7 @@ function set_completion_param { param_name="$1" param_value="$2" - if [ ! $COMPLETION_FILE ]; then + if [ ! "$COMPLETION_FILE" ]; then COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt fi if [ ${#COMPLETION_FILE} -eq 0 ]; then @@ -132,14 +132,14 @@ function set_completion_param { if [ ${#param_name} -gt 0 ]; then if [ ${#param_value} -gt 0 ]; then - if [ -f $COMPLETION_FILE ]; then - if grep -q "${param_name}:" $COMPLETION_FILE; then - sed -i "s|${param_name}:.*|${param_name}:${param_value}|g" $COMPLETION_FILE + if [ -f "$COMPLETION_FILE" ]; then + if grep -q "${param_name}:" "$COMPLETION_FILE"; then + sed -i "s|${param_name}:.*|${param_name}:${param_value}|g" "$COMPLETION_FILE" else - echo "${param_name}:${param_value}" >> $COMPLETION_FILE + echo "${param_name}:${param_value}" >> "$COMPLETION_FILE" fi else - echo "${param_name}:${param_value}" > $COMPLETION_FILE + echo "${param_name}:${param_value}" > "$COMPLETION_FILE" fi fi fi @@ -148,7 +148,7 @@ function set_completion_param { function mark_completed { param_name="$1" - if [ ! $COMPLETION_FILE ]; then + if [ ! "$COMPLETION_FILE" ]; then COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt fi if [ ${#COMPLETION_FILE} -eq 0 ]; then @@ -156,12 +156,12 @@ function mark_completed { fi if [ ${#param_name} -gt 0 ]; then - if [ -f $COMPLETION_FILE ]; then - if ! grep -Fxq "${param_name}" $COMPLETION_FILE; then - echo "${param_name}" >> $COMPLETION_FILE + if [ -f "$COMPLETION_FILE" ]; then + if ! grep -Fxq "${param_name}" "$COMPLETION_FILE"; then + echo "${param_name}" >> "$COMPLETION_FILE" fi else - echo "${param_name}" > $COMPLETION_FILE + echo "${param_name}" > "$COMPLETION_FILE" fi fi } @@ -169,16 +169,16 @@ function mark_completed { function is_completed { param_name="$1" - if [ ! $COMPLETION_FILE ]; then - COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt + if [ ! "$COMPLETION_FILE" ]; then + COMPLETION_FILE="$HOME/${PROJECT_NAME}-completed.txt" fi if [ ${#COMPLETION_FILE} -eq 0 ]; then - COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt + COMPLETION_FILE="$HOME/${PROJECT_NAME}-completed.txt" fi if [ ${#param_name} -gt 0 ]; then - if [ -f $COMPLETION_FILE ]; then - if grep -Fxq "${param_name}" $COMPLETION_FILE; then + if [ -f "$COMPLETION_FILE" ]; then + if grep -Fxq "${param_name}" "$COMPLETION_FILE"; then echo "1" return fi @@ -191,15 +191,11 @@ function read_config_param { param_name="$1" if [ ${#param_name} -gt 0 ]; then - if [ $CONFIGURATION_FILE ]; then - if [ -f $CONFIGURATION_FILE ]; then - if grep -q "${param_name}=" $CONFIGURATION_FILE; then - param_value=$(cat $CONFIGURATION_FILE | grep "${param_name}=" | head -n 1 | awk -F '=' '{print $2}') - if [[ "${param_value}" != *" "* ]]; then - export ${param_name}=${param_value} - else - export ${param_name}="${param_value}" - fi + if [ "$CONFIGURATION_FILE" ]; then + if [ -f "$CONFIGURATION_FILE" ]; then + if grep -q "${param_name}=" "$CONFIGURATION_FILE"; then + param_value=$(grep "${param_name}=" "$CONFIGURATION_FILE" | head -n 1 | awk -F '=' '{print $2}') + export "${param_name}"="${param_value}" fi fi fi @@ -210,10 +206,10 @@ function config_param_exists { param_name="$1" if [ ${#param_name} -gt 0 ]; then - if [ $CONFIGURATION_FILE ]; then - if [ -f $CONFIGURATION_FILE ]; then - if grep -q "${param_name}=" $CONFIGURATION_FILE; then - param_value=$(cat $CONFIGURATION_FILE | grep "${param_name}=" | head -n 1 | awk -F '=' '{print $2}') + if [ "$CONFIGURATION_FILE" ]; then + if [ -f "$CONFIGURATION_FILE" ]; then + if grep -q "${param_name}=" "$CONFIGURATION_FILE"; then + param_value=$(grep "${param_name}=" "$CONFIGURATION_FILE" | head -n 1 | awk -F '=' '{print $2}') if [ ${#param_value} -gt 0 ]; then echo "1" return @@ -229,7 +225,7 @@ function write_config_param { param_name="$1" param_value="$2" - if [ ! $CONFIGURATION_FILE ]; then + if [ ! "$CONFIGURATION_FILE" ]; then CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg fi if [ ${#CONFIGURATION_FILE} -eq 0 ]; then @@ -238,14 +234,14 @@ function write_config_param { if [ ${#param_name} -gt 0 ]; then if [ ${#param_value} -gt 0 ]; then - if [ -f $CONFIGURATION_FILE ]; then - if grep -q "${param_name}=" $CONFIGURATION_FILE; then - sed -i "s|${param_name}=.*|${param_name}=${param_value}|g" $CONFIGURATION_FILE + if [ -f "$CONFIGURATION_FILE" ]; then + if grep -q "${param_name}=" "$CONFIGURATION_FILE"; then + sed -i "s|${param_name}=.*|${param_name}=${param_value}|g" "$CONFIGURATION_FILE" else - echo "${param_name}=${param_value}" >> $CONFIGURATION_FILE + echo "${param_name}=${param_value}" >> "$CONFIGURATION_FILE" fi else - echo "${param_name}=${param_value}" > $CONFIGURATION_FILE + echo "${param_name}=${param_value}" > "$CONFIGURATION_FILE" fi fi fi @@ -255,10 +251,10 @@ function remove_config_param { param_name="$1" if [ ${#param_name} -gt 0 ]; then - if [ $CONFIGURATION_FILE ]; then - if [ -f $CONFIGURATION_FILE ]; then - if grep -q "${param_name}=" $CONFIGURATION_FILE; then - sed -i "/${param_name}=/d" $CONFIGURATION_FILE + if [ "$CONFIGURATION_FILE" ]; then + if [ -f "$CONFIGURATION_FILE" ]; then + if grep -q "${param_name}=" "$CONFIGURATION_FILE"; then + sed -i "/${param_name}=/d" "$CONFIGURATION_FILE" fi fi fi @@ -269,13 +265,13 @@ function remove_completion_param { param_name="$1" if [ ${#param_name} -gt 0 ]; then - if [ $COMPLETION_FILE ]; then - if [ -f $COMPLETION_FILE ]; then - if grep -Fxq "${param_name}" $COMPLETION_FILE; then - sed -i "/${param_name}/d" $COMPLETION_FILE + if [ "$COMPLETION_FILE" ]; then + if [ -f "$COMPLETION_FILE" ]; then + if grep -Fxq "${param_name}" "$COMPLETION_FILE"; then + sed -i "/${param_name}/d" "$COMPLETION_FILE" fi - if grep -q "${param_name}:" $COMPLETION_FILE; then - sed -i "/${param_name}:/d" $COMPLETION_FILE + if grep -q "${param_name}:" "$COMPLETION_FILE"; then + sed -i "/${param_name}:/d" "$COMPLETION_FILE" fi fi fi @@ -291,16 +287,16 @@ function save_configuration_values { function read_configuration_values { # if not installing on a Beaglebone then use sdb as the USB drive by default - if [ ! $INSTALLING_ON_BBB ]; then + if [ ! "$INSTALLING_ON_BBB" ]; then if [[ $USB_DRIVE == /dev/sda1 ]]; then USB_DRIVE=/dev/sdb1 fi fi - if [ -f $CONFIGURATION_FILE ]; then + if [ -f "$CONFIGURATION_FILE" ]; then # Ensure that a copy of the config exists for upgrade purposes if [[ $CONFIGURATION_FILE != "/root/${PROJECT_NAME}.cfg" ]]; then - cp $CONFIGURATION_FILE /root/${PROJECT_NAME}.cfg + cp "$CONFIGURATION_FILE" "/root/${PROJECT_NAME}.cfg" fi for v in "${configuration_variables[@]}" @@ -308,8 +304,9 @@ function read_configuration_values { read_config_param "$v" done - if grep -q "DEBIAN_REPO" $CONFIGURATION_FILE; then + if grep -q "DEBIAN_REPO" "$CONFIGURATION_FILE"; then read_config_param "DEBIAN_REPO" + # shellcheck disable=SC2034 CHECK_MESSAGE=$"Check your internet connection, /etc/network/interfaces and /etc/resolvconf/resolv.conf.d/head, then delete $COMPLETION_FILE, run 'rm -fR /var/lib/apt/lists/* && apt-get update --fix-missing' and run this script again. If hash sum mismatches persist then try setting $DEBIAN_REPO to a different mirror and also change /etc/apt/sources.list." fi fi @@ -317,7 +314,7 @@ function read_configuration_values { function read_configuration { if [[ $INSTALLING_FROM_CONFIGURATION_FILE == "yes" ]]; then - if [ ! -f $CONFIGURATION_FILE ]; then + if [ ! -f "$CONFIGURATION_FILE" ]; then echo $"The configuration file $CONFIGURATION_FILE was not found" exit 8935 fi @@ -336,8 +333,8 @@ function check_system_type { # check that domain names are valid and that they are unique for each app function check_domains { - specified_domains=$(cat $CONFIGURATION_FILE | grep "_DOMAIN_NAME" | uniq) - domains_array=($specified_domains) + specified_domains=$(grep "_DOMAIN_NAME" "$CONFIGURATION_FILE" | uniq) + domains_array=("$specified_domains") checked_domains=() for d in "${domains_array[@]}" @@ -346,8 +343,7 @@ function check_domains { if [[ "$domain_param" != "DEFAULT_DOMAIN_NAME" ]]; then domain_value=$(echo "$d" | awk -F '=' '{print $2}') if [[ "$domain_value" != "${LOCAL_NAME}.local" ]]; then - item_in_array "${domain_value}" "${checked_domains[@]}" - if [[ $? != 0 ]]; then + if ! item_in_array "${domain_value}" "${checked_domains[@]}"; then # test that this is a valid domain name function_check test_domain_name test_domain_name "$domain_value"