执行后,脚本将要求配色方案。配色方案以这种方式以文件为单位:
$ cat schemes
sch-001=red blue white
sch-002=green yellow black
相关的阵列在此处清楚地要求。此外,根据方案,应在另一个(nested)脚本或命令中使用每种颜色。在脚本中,应该像这样。
#!/usr/bin/env bash
... (The initial part of the script) ...
declare -A assscheme
mapfile -O 1 -t inscheme < schemes #I prefer keys starting with 1
for val in "${inscheme[@]}"; do
key=${val%%=*}
value=${val#*=}
assscheme[$key]=$value
done
echo "${!assscheme[@]}"
echo "${assscheme[@]}"
read -p " Select color scheme: " color #Let's say I've selected sch-001
echo "$color"
if [[ "$key" == "$color" ]] ; then
# I can't think of anything further yet.
# And then I need something like this.
magick ......... -fill "$red" ...........
.........................................
script1 .......... -b "$blue" ...........
.........................................
script2 .......... -o "$white" ..........
fi
... (Continue the script) ...
如何以这种方式组织?还是还有另一种方式?
分析解答
一种选项可能是利用grep
和cut
将所选方案的颜色提取到数组中,然后访问颜色的单个数组元素。
#!/bin/bash
read -rp "Select color scheme: " scheme
printf "SCHEME: \n\t%s\nCOLORS: \n" "$scheme"
read -ra colors <<<"$(grep "$scheme" schemes.txt | cut -d= -f2)"
printf '\t%s\n' "${colors[@]}"
# Access the individual array elements as ${colors[0]}, ${colors[1]}, etc.
printf "First color: %s\n" "${colors[0]}"
例子:
$ ./script
Select color scheme: sch-001
SCHEME:
sch-001
COLORS:
red
blue
white
First color: red