Я пишу bash-скрипт для настройки сервера, на котором конечный пользователь должен будет выбрать действия для выполнения, и скрипт выполнит их. Каждое действие будет реализовано как функция.
Я нашел решение для отслеживания зависимостей между этими действиями (используя tsort и tac); упорядоченный список выполняемых функций будет отправлен на стандартный вывод.
Вот код:
# This function accepts the task to be performed as its only argument.
# 1) First, check if we have already added the task to the list. If true,
# bail out to avoid processing it twice.
# 2) If a task does not depend on others, add a "self dependency" so that
# it will appear in the final list, even if no other tasks depend on
# it.
# 3) If a task depends on another one, add the dependency and then call
# adddep() on the second task. That will recursively add all
# dependencies to the list.
# Care should be taken not to introduce infinite cycles.
adddep() {
TASKFOUND=`echo $DEPENDENCIES | grep -F "$1 " | wc -l`
if [ $TASKFOUND -eq 1 ]; then return; fi
case "$1" in
task1)
DEPENDENCIES="${DEPENDENCIES}task1 task1\n"
;;
task2)
DEPENDENCIES="${DEPENDENCIES}task2 task1\n"
adddep task1
;;
task3)
DEPENDENCIES="${DEPENDENCIES}task3 task2\n"
adddep task2
;;
task4)
DEPENDENCIES="${DEPENDENCIES}task4 task1\n"
adddep task1
;;
task5)
DEPENDENCIES="${DEPENDENCIES}task5 task5\n"
;;
esac
}
# Get the list of tasks to be performed. The "3>&1 1>&2 2>&3" at the end
# will switch stdout and stderr (needed because whiptail prints its
# output to stderr).
TASKS=$(whiptail --title "Choose the tasks" --nocancel --separate-output --checklist "Choose the tasks you want to perform from the list below." 20 70 10 task1 "Task 1" 0 task2 "Task 2 (depends on Task 1)" 0 task3 "Task 3 (depends on Task 2)" 0 task4 "Task 4 (depends on Task 1)" 0 task5 "Task 5" 0 3>&1 1>&2 2>&3)
# Build a dependency graph (an edge goes from A to B iff performing the
# task A requires the task B to be performed first).
DEPENDENCIES=""
for i in $TASKS; do
adddep $i
done
# DEPENDENCIES now contains a list of all edges we need to keep track of,
# topologically sort the graph to get the lists of tasks to perform in
# reverse order (via "tsort") and reverse the order (via "tac").
ORDEREDTASKS=`echo $DEPENDENCIES | tsort | tac`
# Perform the tasks.
for i in $ORDEREDTASKS; do
# Call the functions listed in $ORDEREDTASKS
done
Как я могу сказать bash вызвать список функций, предоставляемых в потоке (одна функция на строку)?