linux - 嵌套函数和 while 循环在 bash 中返回 `while: command not found`

标签 linux bash shell gentoo

<分区>

主要的函数execSteps一个一个的执行emerge --pretend $package,这些包的名字(只有名字,没有版本信息)被保存在一个文本文件中步骤文件。一些包可能额外需要配置package.use,package.license,这种额外信息会在执行emerge --pretend $package后显示。 main 函数中的第二个 while 循环和 function acceptPKGTypeItems 就是为了处理这种额外的信息。

当出现一个特定的包时,它可能依赖于更多的包。例如emerge --pretend ceph,在ceph出现之前,我需要emerge 10多个包。随着 Gentoo/Linux 的更新,可能会应用新版本的软件包。所以文本文件 stepFile 只包含我需要的包名称,并解析 emerge --pretend $package 的结果,我能够得到更新的包出现。

case 0),这个while循环是为了解析emerge --pretend $line(which is from stepFile)的结果,例如 emerge --pretend ceph 并获取其当前版本的依赖包,例如 dev-libs/boost-1.57.0,将其作为参数传递给 function emgRecursion 因为 ceph 包的依赖包 dev-libs/boost-1.57.0 可能有自己的依赖包,它们是 dev-libs/boost-build- 1.57.0dev-libs/boost-1.57.0

我的问题是,当我在 case 0) 中输入 0 时,在 function emgRecursion 中出现错误 while : command not found。这是另一个不同的 shell 吗?我在 main 函数的第二个 while 循环之间添加了一对括号,这有助于从用户那里获得有关选择 package.use、package.license 或 package.keywords 的阅读答案。而且我尝试在第三个while循环之间添加另一对括号,同样的问题。我已经分别测试了 emgRecursionacceptPKGTypeItems,它们都可以正常工作。

有什么想法吗?非常感谢。

function acceptPKGTypeItems() {
...
}

function emgRecursion() {
    local output="$(emerge --pretend "="$1 | grep "\[ebuild")"

        while read -r line;
        do

        done <<<"$output"
}
function execSteps() {
        local running=0
        while read -r line;
        do
        if (( running )); then
              if [[ $line = "#"* ]] && [[ "${line/"step"}" = "$line" ]]; then
                continue
              else
                if [[ ! "${line/"step"}" = "$line" ]]; then
                        echo "======      approaching to the next step which is not available at this time."
                        break
                else
(                       output="$(emerge --pretend $line | grep "\[ebuild")"
                        echo "****************  $line is ready for emerging   ****************"
                        while read -p "Which type of package would you like to add new item to (1-packageuse 2-packagelicense 3-packagekeywords 0-exit and continue)? " choice; do
                        case "$choice" in
                                1) echo "****************  $line is ready for emerging"
                                        acceptPKGTypeItems $PACKAGEUSE
                                        echo "****************   package.use has been updated."

                                ;;
                                2) echo "****************  $line is ready for emerging"
                                        acceptPKGTypeItems $PACKAGELICENSE
                                        echo "****************   package.license has been updated."

                                ;;
                                3) echo "****************  $line is ready for emerging"
                                        acceptPKGTypeItems $PACKAGEKEYWORDS
                                        echo "****************   package.keywords has been updated."
                                ;;
                                0) echo "****************  $line starts emerging"

                                        while read -r element;
                                        do
                                            local str="${element#*"] "}"
                                                str="${str%%" "*}"
                                                echo "      $str is an element that need to be emerged.      "
                                                emgRecursion "$str"
                                        done <<<"$output"
                                        echo "****************  $line has been emerged.   ****************"
                                        break
                                ;;
                                *) echo "~~~~~~~~~~~~~~~~   Invalid input, try again.   ~~~~~~~~~~~~~~~~"
                                ;;
                        esac
                        done) </dev/tty
                fi
              fi
            else
                 [[ $line = "#"$1 ]] && running=1
        done <$STEPS
}

execSteps step2

在main函数中while循环什么都不会停止, 输出:

livecd / # ./step1
 * Last emerge --sync was 32d 23h 4m 58s ago.
****************  sys-cluster/ceph is ready for emerging   ****************
Which type of package would you like to add new item to (1-packageuse 2-packagelicense 3-package.keywords 0-exit and continue)?0
****************  sys-cluster/ceph starts emerging   ****************
      dev-libs/libaio-0.3.110 is an element that need to be emerged.      
 * Last emerge --sync was 32d 23h 5m 3s ago.
./step1: line 48:        while: command not found
./step1: line 49:        : command not found
./step1: line 50:                str=dev-libs/libaio-0.3.110: No such file or directory
 Take a look at what dev-libs/libaio-0.3.110 looks like.
./step1: line 77:        done: command not found
      sys-libs/libunwind-1.1 is an element that need to be emerged.      
 * Last emerge --sync was 32d 23h 5m 5s ago.
^C

Exiting on signal 2

最佳答案

在我创建用于测试 emgRecursion 时,通过将 emgRecursion 以外的函数复制到另一个文件来解决问题。

我意识到这两个文件之间的区别(recursion 用于测试 emgRecursionstep 用于测试整个函数)是 recursion 最初是用#!/bin/bash创建的,而step最初是一个没有任何首行符号的纯shell文本文件,然后我添加了 #!/bin/bash 到它。我认为 bash 文本文件和 shell 文本文件在语法方面没有太大区别。事实上,它们完全不同。如果像我这样把它们混在一起,那是在浪费时间。

关于linux - 嵌套函数和 while 循环在 bash 中返回 `while: command not found`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33882343/

相关文章:

linux - 在 Linux bash 中, sleep 一定的数字会永远持续下去

linux - 为什么 CUDA 的示例联编文件找不到 CUDA 库?

ruby - 如何确定 PTY.spawn 何时在 ruby​​ 脚本中完成,然后开始新进程

linux - 给定行分隔的文本 block ,如何返回包含特定关键字的每个 block ?

linux - 获取 bash : syntax error near unexpected token `(' in curl

java - shell 脚本: running a java application and sending command-line args to it via the script?

linux - 使用 grep 搜索包含一些文本的文件

c++ - 如何使用 C++ 继承和扩展 QCalendarWidget

bash - 在 Unix 中使用 bash 查找指定大小的文件

regex - 在egrep中查找某组数字