简体   繁体   English

使用If / Then / Else语句在BASH脚本中调用函数

[英]Call Function in BASH Script using If/Then/Else Statement

I'm trying to call a function in script by using an if/then/else statement, but I keep getting no output when I run it. 我正在尝试使用if / then / else语句在脚本中调用函数,但是运行它时我一直没有得到任何输出。 For example: 例如:

    start()
    {
    if [ -z /etc/redhat-release ]
       then mta
    else cron
    fi
    }

    mta()
    {
    exim -bpc
    }

    cron()
    {
    crontab -l
    }

Is this possible to do in BASH? 这可以在BASH中进行吗?

Note: 注意:

  1. Declare function before the function which calls it, this is always a good habit; 在调用它的函数之前声明函数,这总是一个好习惯;
  2. cron is a common command in Unix/Linux system, use another name for your function please. cron是Unix / Linux系统中的常用命令,请为您的函数使用另一个名称。

      #!/bin/bash mta() { # your code here } mycron() { # your code here } start() { if [ -z /etc/redhat-release ]; then mta else mycron fi } start 

Sure, this is possible you just need to define the function, before you call it first. 当然,您可以只在第一次调用函数之前就定义它。 In this case you have to define the functions called in the start function before the start function. 在这种情况下,必须在启动功能之前定义启动功能中调用的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM