0

I have store environment variable names in an array with name array and I want to check if it is set then only certain code should run. Like if somebody has set environment variable db_instance_type="t2.micro" then desire output should be -var="db_instance_type=$db_instance_type"

I have written below code but it is not working as expected because I am checking variable presence using single $ which will always be there and not checking main variable value.

set -u
db_instance_type="t2.micro" # for testing only
db_dns_ttl="300"    # for testing only
function check_input_vars() {
    local temp_cmd=''

    declare -a array=(db_instance_type db_vpc_id db_subnet_ids db_sg_cidr_allowed_range db_ebs_volume_size delete_on_termination_db_ebs_volume
                    security_group_id contact db_image_owner_id db_dns_ttl db_image_name_filter ws_key_name ws_dns_ttl ws_instance_type
                    ws_vpc_id  ws_subnet_ids ws_ami_id enable_ws_public_ip
                    )

    temp_cmd=''
    for var in "${array[@]}"
    do
        var_name=(${!var@})
        if [[ "${var_name}" ]]; then 
            temp_cmd+=" -var=\"$var=\$$var\""
        fi
    done   
    echo $temp_cmd
}

input_var_str="$(check_input_vars)"
echo "$input_var_str"  # output should be: -var="db_instance_type=$db_instance_type" -var="db_dns_ttl=$db_dns_ttl
unset db_instance_type  # for testing only

Fix:

I have to comment this line --> #set -u And I have used below code:

var_value=${!var}
if [[ ! -z "${var_value}" ]]; then temp_cmd+=" -var=\"$var=\$$var\""; fi
3
  • Subjectively: This sounds like a bad idea. I believe using so many global variables will only result in unmaintanable spaghetti code. unset db_instance_type # for testing only spagetti. Commented Sep 15, 2021 at 11:29
  • You want the output to be exactly literally -var="db_instance_type=$db_instance_type" or do you want the output ot be -var="db_instance_type=t2.micro"? You specified the first, so the command works like you want, so? Also why do you want to have " in the string - are you sure you want quotes preserved literally? Commented Sep 15, 2021 at 11:31
  • output should be: -var="db_instance_type=$db_instance_type" -var="db_dns_ttl=$db_dns_ttl Commented Sep 15, 2021 at 11:54

1 Answer 1

1

The syntax is off - to get the variable value use ${!var}. No @. And ( ) are for array assignment - it's not an array, it's a single vlaue.

    var_value=${!var}
    if [[ -n "${var_value}" ]]; then..

Also, it accesses the value of the variable, where variable var stores the name of the variable, thus I would call the variable var_value.

Check your scripts with https://shellcheck.net .

To protect against set -u, expand variable to empty when not defined or empty.

 var_value=${!var:-}

Also: How to check if a variable is set in Bash?


output should be: -var="db_instance_type=$db_instance_type" -var="db_dns_ttl=$db_dns_ttl"

Related reading: https://mywiki.wooledge.org/BashFAQ/048 https://mywiki.wooledge.org/BashFAQ/050 https://mywiki.wooledge.org/BashFAQ/006 .

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

2 Comments

I am not getting right output and also getting an error. !var: unbound variable (blank)
Hi @kmilChuk, I figure out whats the issue here. The issue is` set -u` . When I removed it it work fine otherwise it fail.

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.