调用流上提供的函数列表

调用流上提供的函数列表

我正在编写一个 bash 脚本来配置服务器,最终用户必须选择要执行的操作,然后脚本将执行这些操作。每个操作都将作为一个函数来实现。

我找到了一个解决方案来跟踪这些操作之间的依赖关系(使用排序战术);要执行的函数的有序列表将被发送到 stdout。

以下是代码:

# 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 调用流上提供的函数列表(每行一个函数)?

答案1

要读取您想要使用的标准输入(流/管道?)

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

相关内容