0

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. The string is:

If the $httpd_server_count=1:

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

If the $httpd_server_count=2:

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

and count can be is up to 10 and more. How to properly organize it in code?

3
  • 1
    How to properly organize it in code What is wrong with this and why do you want a new one? Commented Jan 9, 2017 at 8:57
  • the main reason I don't want to duplicate the code, if the max count is 50 then it will be very big if statement. Commented Jan 9, 2017 at 9:00
  • Could you please show an example how it would be with an array? Commented Jan 9, 2017 at 9:09

3 Answers 3

3

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. If I have it right, one way would be to use a simple C-Style for loop based on httpd_server_count. 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:

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

If it is 2, you get:

{'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)

Sign up to request clarification or add additional context in comments.

1 Comment

++ would have done the same way! Not posting it as a separate one. BTW using an array would slightly look neater. But a proper JSON tool would be the ideal way.
3

Use functions in bash to make the code more portable and efficient. Just put the code in a wrapper like below. Logic is similar to this David C. Rankin's answer,

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=$(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[@]}"
}

Comments

2

Use for loop:

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

1 Comment

No, it is also working for strings. man bash: In the context where an assignment statement is assigning a value to a shell variable or array index, the += operator can be used to append to or add to the variable's previous value. This includes arguments to builtin commands such as declare that accept assignment statements (declaration commands). [...] When += is applied to an array variable[...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.