简体   繁体   English

根据另一个变量将字符串值添加到变量

[英]Add string values to variable depending on another variable

I have the following variable in bash: $httpd_server_count, this variable can contain values like 1 or 2 and etc. And depending on this value I need to get proper value in $settings variable. 我在bash中有以下变量:$ httpd_server_count,该变量可以包含1或2等值,并且根据此值,我需要在$ settings变量中获取适当的值。 The string is: 字符串是:

If the $httpd_server_count=1: 如果$ httpd_server_count = 1:

settings={'httpd1': {'server': 'settings.server1'},

If the $httpd_server_count=2: 如果$ httpd_server_count = 2:

settings={'httpd1': {'server': 'settings.server1'}, {'httpd2': {'server': 'settings.server2'},

and count can be is up to 10 and more. 最多可以有10个或更多。 How to properly organize it in code? 如何在代码中正确地组织它?

I'm a bit unclear on what you are needing, but it appears you want to read the value of httpd_server_count and based on the number 1, 2, whatever generate the server strings needed. 我不清楚您需要什么,但似乎您想读取httpd_server_count的值,并根据数字1, 2, whatever生成所需的服务器字符串。 If I have it right, one way would be to use a simple C-Style for loop based on httpd_server_count . 如果我做对了,一种方法是使用基于httpd_server_count的简单C风格for循环。 For example: 例如:

#!/bin/bash

str=
for ((i = 1; i <= $httpd_server_count; i++)); do
    str="${str}{'httpd$i': {'server': 'settings.server$i'}, "
done

echo -e "str\n\n$str"

You would still be responsible for trimming the trailing , and appending a closing '}' if required. 您仍将负责修剪结尾处,并在需要时附加结尾的'}' But, if your value of httpd_server_count is 1 , then you get: 但是,如果您的httpd_server_count值为1 ,那么您将得到:

{'httpd1': {'server': 'settings.server1'},

If it is 2 , you get: 如果为2 ,则得到:

{'httpd1': {'server': 'settings.server1'}, {'httpd2': {'server': 'settings.server2'},

This may give you an idea you can run with. 这可能会给您一个可以运行的想法。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。 (by the way, a json parser is generally preferred, like jq ) (顺便说一下,通常首选json解析器,例如jq

Use functions in bash to make the code more portable and efficient. bash使用函数可使代码更可移植且更有效。 Just put the code in a wrapper like below. 只需将代码放入下面的包装器中即可。 Logic is similar to this David C. Rankin's answer , 逻辑与此David C. Rankin的答案相似,

function settingsGenerator() {   
    local httpd_server_count=$1

    local i
    local settings

    for ((i=1; i<=httpd_server_count; i++))
    do
        settings="${settings}{'httpd$i': {'server': 'settings.server$i'}, "
    done

    printf "%s" "$settings"
}

and store the output of the function in a variable, settings as 并将函数的输出存储在变量中, settings

settings=$(settingsGenerator 2)
printf "%s\n" "$settings"
{'httpd1': {'server': 'settings.server1'},{'httpd2': {'server': 'settings.server2'},

Using arrays would require a small change something like, 使用数组需要做一些小的改动,例如,

function settingsGenerator() {   
    local httpd_server_count=$1

    local i
    local settings=()

    for ((i=1; i<=httpd_server_count; i++))
    do
        settings+="{httpd$i': {'server': 'settings.server$i'}, "
    done

    printf "%s" "${settings[@]}"
}

Use for loop: 用于循环:

#!/bin/bash
for i in `seq $httpd_server_count`; do
    settings+="{'httpd$i': {'server': 'settings.server$i'}, "
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM