Chamando uma lista de funções fornecidas em um fluxo

0

Estou escrevendo um script bash para configurar um servidor no qual o usuário final terá que escolher as ações a serem executadas e o script as executará. Cada ação será implementada como uma função.

Encontrei uma solução para acompanhar as dependências entre essas ações (usando tsort e tac ); a lista ordenada de funções a serem executadas será enviada para a stdout.

Aqui está o código:

# 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

Como posso dizer ao bash para chamar uma lista de funções fornecidas em um fluxo (uma função por linha)?

    
por Alessandro Menti 27.06.2014 / 12:27

1 resposta

2

Para ler stdin (stream / piping?) você quer usar

function myfunction {
   while read line;
   do
     echo "$line"
     $line  #will execute as is
   done
}

#call your function with
echo "ls -l\nrm foobar\necho all done" | myfunction
    
por 27.06.2014 / 13:54