Home > Blockchain >  Bash: Escaping @ in string value in non-associative array
Bash: Escaping @ in string value in non-associative array

Time:06-15

I am writing a sanitation script to loop through a list of allowed key names to match variables. The variables are email addresses using '@' in the name. I am having a tough time trying to escape out the '@' symbol so that bash isn't trying to use it as a reference point. Could someone help me out. I am probably missing something simple.

#!/usr/bin/env bash

george="[email protected]"
mike="[email protected]"
john="[email protected]"

people_key_names=( "george" "mike" "john" )
master_key_values=( "[email protected]" 'cluster2-msid2\@somedomain.com' "cluster5-msid3`@`somedomain.com")

function print_emails(){
    for person in ${people_key_names[@]}; do
        value=$(echo "${!master_key_values["${person}"]}")
        echo "$value"
    done
}

print_emails;

Error message being received:

./example.sh: line 12: [email protected]: syntax error: invalid arithmetic operator (error token is "@somedomain.com")

./example.sh: line 12: [email protected]: syntax error: invalid arithmetic operator (error token is "@somedomain.com")

./example.sh: line 12: [email protected]: syntax error: invalid arithmetic operator (error token is "@somedomain.com")

From the answer, the updated code for what I was actually intending once mistake was realized:

#!/usr/bin/env bash

george="[email protected]"
mike="[email protected]"
john="[email protected]"

people_key_names=( "george" "mike" "john" )
master_key_values=( "[email protected]" 'cluster2-msid2\@somedomain.com' "cluster5-msid3`@`somedomain.com")

function print_emails(){
    people_success=0
    for key_value in ${master_key_values[@]}; do
        for person_value in ${people_key_names[@]}; do
            echo "Key_Value: $key_value / $person_value : Declared_variable: ${!person_value}"
        done
    done
}

print_emails;

CodePudding user response:

Why not using associative array?

#!/bin/bash

declare -A arr
arr[george]="[email protected]"
arr[mike]="[email protected]"
arr[john]="[email protected]"
declare -p arr


for key in "${!arr[@]}"; do echo "key: $key ==> value: ${arr[$key]}"; done

output

declare -A arr=([john]="[email protected]" [mike]="[email protected]" [george]="[email protected]" )

key: john ==> value: [email protected]
key: mike ==> value: [email protected]
key: george ==> value: [email protected]

CodePudding user response:

You have 2 numerically index arrays. To iterate over them, we loop over the indices of one of them:

    for idx in "${!people_key_names[@]}"; do
        echo "person ${person[idx]} has value ${master_key_values[idx]}"
    done

CodePudding user response:

Unless you're on macOS then you should use an associative array:

#!/bin/bash

declare -A emails=(
    [george]="[email protected]"
    [mike]="[email protected]"
    [john]="[email protected]"
)

print_emails() {
    local person
    for person
    do
        printf '%s: %s\n' "$person" "${emails["${person,,}"]:--}"
    done
}

print_emails MIKE john charles
MIKE: [email protected]
john: [email protected]
charles: -
  •  Tags:  
  • bash
  • Related