有没有办法创建一个函数来改变基于数组的布尔值?
Is there a way to create a function that changes booleans values based off array?
我的脚本中有不同的安装提示,可以通过布尔值切换。我想将每个相应的布尔值放入像数组这样的容器数据类型中,以便我可以对它们进行分类和稍后修改。更具体地说,我想使用一个函数从 array/list 中修改我的布尔值。 我是否必须使用索引值或其他东西而不是可迭代值来修改项目 i ?提前致谢。
#!/bin/bash
# this is example code based off my installation script.
INSTALL_GIMP=0
INSTALL_KRITA=0
INSTALL_VLC=0
images_array=($INSTALL_GIMP $INSTALL_KRITA $INSTALL_VLC)
enabling_prompts() {
arg1=
for i in ${arg1[@]} ; do
echo "$i"
i=1
echo "$i"
done
}
# ^ I want a function like this to change the given array's booleans to 1.
# I want to use modifiable arrays to make it easier when adding
# -more categorized "programs" to the prompt script.
enabling_prompts $images_array
while [[ $INSTALL_GIMP == 1 ]]
do
echo "This is skeleton code. I need to change the values to 1 so that this code can be run."
done
您可以将布尔值存储到关联数组中,并在函数内通过引用访问该数组:
#!/usr/bin/env bash
# Associative array of boolean
declare -iA images_array=(
["gimp"]=0
["krita"]=0
["vlc"]=0
)
# The array name is passed by reference as argument 1
enabling_prompts() {
# array is a reference name (Need Bash 4.2+)
local -n array=""
for prog in "${!array[@]}"; do
printf -v prompt 'Install %s (Y)es|(N)o?\n' "$prog"
while read -r -p "$prompt" answer; do
case "$answer" in
[yY])
array["$prog"]=1
break
;;
[nN])
break
;;
*) :
;;
esac
:
done
done
}
# ^ I want a function like this to change the given array's booleans to 1.
# I want to use modifiable arrays to make it easier when adding
# -more categorized "programs" to the prompt script.
enabling_prompts 'images_array'
for prog in "${!images_array[@]}"; do
if [ "${images_array["$prog"]}" -eq 1 ]; then
printf 'Installing %s...\n' "$prog"
else
printf '%s will not be installed!\n' "$prog"
fi
done
我的脚本中有不同的安装提示,可以通过布尔值切换。我想将每个相应的布尔值放入像数组这样的容器数据类型中,以便我可以对它们进行分类和稍后修改。更具体地说,我想使用一个函数从 array/list 中修改我的布尔值。 我是否必须使用索引值或其他东西而不是可迭代值来修改项目 i ?提前致谢。
#!/bin/bash
# this is example code based off my installation script.
INSTALL_GIMP=0
INSTALL_KRITA=0
INSTALL_VLC=0
images_array=($INSTALL_GIMP $INSTALL_KRITA $INSTALL_VLC)
enabling_prompts() {
arg1=
for i in ${arg1[@]} ; do
echo "$i"
i=1
echo "$i"
done
}
# ^ I want a function like this to change the given array's booleans to 1.
# I want to use modifiable arrays to make it easier when adding
# -more categorized "programs" to the prompt script.
enabling_prompts $images_array
while [[ $INSTALL_GIMP == 1 ]]
do
echo "This is skeleton code. I need to change the values to 1 so that this code can be run."
done
您可以将布尔值存储到关联数组中,并在函数内通过引用访问该数组:
#!/usr/bin/env bash
# Associative array of boolean
declare -iA images_array=(
["gimp"]=0
["krita"]=0
["vlc"]=0
)
# The array name is passed by reference as argument 1
enabling_prompts() {
# array is a reference name (Need Bash 4.2+)
local -n array=""
for prog in "${!array[@]}"; do
printf -v prompt 'Install %s (Y)es|(N)o?\n' "$prog"
while read -r -p "$prompt" answer; do
case "$answer" in
[yY])
array["$prog"]=1
break
;;
[nN])
break
;;
*) :
;;
esac
:
done
done
}
# ^ I want a function like this to change the given array's booleans to 1.
# I want to use modifiable arrays to make it easier when adding
# -more categorized "programs" to the prompt script.
enabling_prompts 'images_array'
for prog in "${!images_array[@]}"; do
if [ "${images_array["$prog"]}" -eq 1 ]; then
printf 'Installing %s...\n' "$prog"
else
printf '%s will not be installed!\n' "$prog"
fi
done