在bash脚本中调用函数时遇到问题 [英] Having problems calling function within bash script

查看:104
本文介绍了在bash脚本中调用函数时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在进行介绍性脚本编写工作,在调用脚本中的函数时遇到问题.我处于作业的第二部分,我只是在进行测试以确保(希望)我的所有东西都能正常工作.我已经收集了一些目录,并询问是或否的问题.当我得到一个"y"时,我编写了一个小函数,当我得到"n"时,我编写了另一个函数,两个简单的回声.有什么问题吗?

I've been working on our intro scripting assignment, and am having issues calling functions within the script. I am in the second portion of the assignment, and I am just testing to make sure what I have is (hopefully) going to work. I have gathered some directories, and ask a yes or no question. When I get a 'y', I wrote a little function that I call, and when I get a 'n' I have another function, both simple echoes. What is the issue?

part_two(){
    answer=""
    for value in "$@";do
      echo "$value"
      while [ "$answer" != "y" -a "$answer" != "n" ]
      do 
        echo -n "Would you like to save the results to a file? (y/n): "
        read answer
      done
      if [ "$answer" = "n" ]
        then 
          part_six
      elif [ "$answer" = "y" ]
        then 
          part_five
      fi 
    done
}

part_two $@

part_five(){
    echo -n "working yes";
}

part_six(){
    echo -n "working no";
}

一如既往,任何帮助将不胜感激.

Any help would be greatly appreciated, as always.

推荐答案

很像在C语言中,必须在使用函数之前对其进行定义.在您的代码段中,您将在声明两个函数之前调用part_two(分别称为part_five和part_six).

Much like in C a function must be defined before it's used. In your code snippet you are calling part_two (which is calling part_five and part_six) before declaring the two functions.

您是否尝试过将其定义移至脚本的开头?

Have you tried moving their definitions to the start of the script?

在大多数情况下,在Bash中处理此问题的最佳方法是在执行任何实际命令之前,在脚本开头简单地定义所有功能.定义的顺序并不重要-壳仅在要使用它时才查找一个函数-因此通常不存在依赖项等问题,您可能需要考虑.

In most cases, the best way to deal with this in Bash is to simply define all functions at the start of the script before executing any actual commands. The order of the definitions does not really matter - the shell only looks up a function when it's about to use it - so generally there are no dependency issues etc. that you may have to think about.

在某些情况下,您可能无法仅在脚本开始时定义函数.一种常见的情况是,当您使用条件构造来动态选择或修改函数的声明时,例如:

There are cases where you may not be able to just define a function at the start of the script. A common case is when you use conditional constructs to dynamically select or modify the declaration of a function e..g.:

if [[ "$1" = 0 ]]; then
    function show() {
        echo Zero
    }
else
    function show() {
        echo Not-zero
    }
fi

在这种情况下,您必须确保每个函数调用在声明该函数(及其所调用的其他函数)之后发生.

In these cases you have to make sure that each function call happens after that function (and any others that it calls) is declared.

在bash中,函数声明实际上是function foo() { ... }块,您可以在其中定义其实现-是的,function关键字不是严格必需的.没有像C中那样的函数原型-它们仍然毫无意义,因为shell脚本通常在执行时进行解析.较新的Bash版本确实可以一次读取脚本,但是它们主要检查语法错误,而不检查诸如此类的逻辑错误.

In bash a function declaration is actually the function foo() { ... } block where you define its implementation - and yes, the function keyword is not strictly necessary. There are no function prototypes as in C - they would not make sense anyway because shell scripts are generally parsed as they are executed. Newer Bash version do read a script at once, but they mostly check for syntax errors and not for logical errors such as this one.

BTW的正式术语是函数 declaration ",但即使是Bash信息页面也可以互换使用"declaration"和"definition".

BTW the official term is "function declaration", but even the Bash info page uses "declaration" and "definition" interchangeably.

这篇关于在bash脚本中调用函数时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆