J'écris un script bash pour configurer un serveur sur lequel l'utilisateur final devra choisir les actions à exécuter et le script les exécutera. Chaque action sera mise en œuvre en tant que fonction.
J'ai trouvé une solution pour garder une trace des dépendances entre ces actions (en utilisant tsort et tac ); la liste ordonnée des fonctions à exécuter sera envoyée à stdout.
Voici le code:
# 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
Comment puis-je dire à bash d'appeler une liste de fonctions fournies sur un flux (une fonction par ligne)?
linux
bash
bash-scripting
Alessandro Menti
la source
la source
Réponses:
Pour lire stdin (stream / piping?) Que vous voulez utiliser
la source