BASH - reading in a configuration file with multiple instances of the same "variable"

I try to read the configuration file, and then put the “section” of the configurations in an array in the bash script, and then run the command and then go through the configs again, and continue to do this until the end of the configuration file.

Here is an example configuration file:

PORT = "5000"
USER = "nobody"
PATH = "1"
OPTIONS = ""

PORT = "5001"
USER = "nobody"
PATH = "1"
OPTIONS = ""

PORT = "5002"
USER = "nobody"
PATH = "1"
OPTIONS = ""

I want the bash script to read in the first "section", inject it into the script, and run the following:
scriptname -p $PORT -u $USER -P $PATH -o $OPTIONS

, , , "" , "" , , , :

scriptname -p $PORT -u $USER -P $PATH -o $OPTIONS
scriptname -p $PORT -u $USER -P $PATH -o $OPTIONS
scriptname -p $PORT -u $USER -P $PATH -o $OPTIONS

:

scriptname -p 5000 -u nobody -P 1 -o ""
scriptname -p 5001 -u nobody -P 1 -o ""
scriptname -p 5002 -u nobody -P 1 -o ""

.

+3
3
#!/bin/bash

if [[ $# -ne 1 ]]; then
    echo "Usage: $0 script.cfg" >&2
    exit 1
fi

function runscript() {
    scriptname -p $PORT -u $USER -P $PATH -o $OPTIONS
}

while read LINE; do
    if [[ -n $LINE ]]; then
        declare "$LINE"
    else
        runscript
    fi
done < "$1"

runscript

, :

function runscript() {
    nohup scriptname -p $PORT -u $USER -P $PATH -o $OPTIONS &> /dev/null &
}

& , nohup , , . , , , script.

+3
#!/bin/bash

awk 'BEGIN{ FS="\n";RS=""}
{
  for(i=1;i<=NF;i++){
   gsub(/.[^=]*=|\042/,"",$i)
  }
  print "scriptname -p "$1" -u "$2" -P "$3" -o "$4
}' file | bash
+2

Assuming there is only one blank line between sections:

cat <yourfile> | while read ; do
    if [ -z "$REPLY" ] ; then
        scriptname -p $PORT -u $USER -P $PATH -o "$OPTIONS" 
    else
        eval "$REPLY" # NOTE: eval is evil
    fi
done
0
source

Source: https://habr.com/ru/post/1758102/


All Articles