سؤال

Is there an easy way of returning the values of a checklist?

Like enter the menu (below) select "fast","lake" and "car", so that everything is turned "on". Leave the menu and next time you reenter it, the same selections are restored?

#! /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.$$
هل كانت مفيدة؟

المحلول

In order to restore the selections, we just have to read them from the file they were saved to (in your example tmp.$$) and insert on at the appropriate places in the dialog command. We can use an associative array for that.

#! /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.$$
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top