Вы сравниваете строку ("A _переменная" )с целым числом ("1" ).
Вы можете обойти эту проблему с помощью declare -n
. Изhelp declare
:
-n make NAME a reference to the variable named by its value
Нам нужно [[.. ]]
, чтобы избежать ошибки, которую вызывает неустановленная «переменная C _».
#!/bin/bash
array=("A" "B" "C")
A_variable=1
B_variable=2
for letter in "${array[@]}"
do
declare -n foo="${letter}_variable"
if [[ "$foo" -eq 1 ]]; then
echo "Ding"
fi
done
Бонус :можно declare -i A_variable=1
сделать целым.
-i to make NAMEs have the `integer' attribute