是否有简单方式返回核对清单的值?
如进入菜单(如下),选择" fast"," lake"和" car",以便一切都在" on"。离开菜单,下次重新输入时,会恢复相同的选择吗?
#! /bin/bash
dialog --checklist "package timing" 20 75 5 \
"tree" "4 MB" on \
"dog" "2 MB" on \
"fast" "5 MB" off \
"lake" "2 MB" off \
"car" "3 MB" off 2> ./tmp.$$
答案 0 :(得分:1)
为了恢复选择,我们只需要从保存到的文件中读取它们(在您的示例tmp.$$
中),然后在on
的适当位置插入dialog
命令。我们可以使用关联数组。
#! /bin/bash
declare -A status=([dog]=on [tree]=on) # initialize "tree" and "dog" to be on
while
dialog --checklist "package timing" 20 75 5 \
"tree" "4 MB" "${status[tree]}" \
"dog" "2 MB" "${status[dog]}" \
"fast" "5 MB" "${status[fast]}" \
"lake" "2 MB" "${status[lake]}" \
"car" "3 MB" "${status[car]}" 2>tmp.$$
do : whatever you want
set -- $(<tmp.$$); set -- ${@/#/[}; set -- ${@/%/]=on}
eval declare -A status=($@)
done
rm tmp.$$