BASH - 阅读与同一&QUOT的多个实例配置文件;变" [英] BASH - Read in config file with multiple instances of the same "variable"

查看:99
本文介绍了BASH - 阅读与同一&QUOT的多个实例配置文件;变"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取配置文件,然​​后CONFIGS的部分放到一个bash脚本的数组,然后运行命令关闭该,然后通过再次CONFIGS reitterate,并继续做这一点,直到配置文件的末尾。

下面是一个示例配置文件:


PORT =5000
USER =没有人
PATH =1
OPTIONS =PORT =5001
USER =没有人
PATH =1
OPTIONS =PORT =5002
USER =没有人
PATH =1
OPTIONS =

我想在bash脚本的第一个节,在读,把它纳入了脚本,运行以下命令:结果
脚本名-p $ PORT -u $ USER-P $ PATH -o $选项

不过,我希望它的基础上,有多少节,也有在配置文件,采取一节的每一次迭代,以及与其相应的配置设置,运行命令并将其应用到了最后的命令。所以,如果它是在从上面的配置文件读取,输出将会是:


脚本名-p $ PORT -u $ USER-P $ PATH -o选项$
脚本名-p $ PORT -u $ USER-P $ PATH -o选项$
脚本名-p $ PORT -u $ USER-P $ PATH -o选项$

这反过来会看起来像:


脚本名-p 5000 -u无人-P 1 -o
脚本名-p 5001 -u无人-P 1 -o
脚本名-p 5002 -u无人-P 1 -o

先谢谢了。


解决方案

 #!/斌/庆典如果[[$#-ne 1];然后
    回声用法:$ 0 script.cfg>和2
    1号出口
科幻功能的runScript(){
    脚本名-p $ PORT -u $ USER-P $ PATH -o选项$
}而读线;做
    如果[[-n $ LINE]];然后
        申报$ LINE
    其他
        的runScript
    科幻
完成< $ 1的runScript


如果您想要同时在后台运行脚本,试试这个:

 函数的runScript(){
    nohup的脚本名-p $ PORT -u $ USER-P $ PATH -o $选项和放大器;>的/ dev / null的&安培;
}

&安培; 末使它们在后台运行和的nohup 可确保他们不会在杀死shell退出。其净效应是把脚本转换为守护进程,这样他们就会在后台持续运行,无论发生什么情况父脚本。

I'm trying to read a config file, and then place the "section" of configs into an array in a bash script, and then run a command off that, and then reitterate through the configs again, and continue to do this until the end of the config file.

Here's a sample config 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", bring it into the script, and run the following:
scriptname -p $PORT -u $USER -P $PATH -o $OPTIONS

HOWEVER, I want it to, based on how many "sections" there are in the config file, to take each iteration of a "section", and run the command with its corresponding configuration settings and apply it to the final command. So if it were to read in the config file from above, the output would be:

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

Which in turn would look like:

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

Thanks in advance.

解决方案

#!/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


If you want to run the scripts in the background simultaneously, try this:

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

The & at the end makes them run in the background and nohup ensures they're not killed when the shell exits. The net effect is to turn the scripts into daemons so they'll run continuously in the background regardless of what happens to the parent script.

这篇关于BASH - 阅读与同一&QUOT的多个实例配置文件;变&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆