bash 后台程序_如何向您的Bash程序添加帮助工具

bash 后台程序

在本系列的第一篇文章中,您创建了一个很小的单行Bash脚本,并探讨了创建Shell脚本的原因,以及为什么它们是系统管理员而非编译程序最有效的选择。 在第二篇文章中 ,您开始了创建一个相当简单的模板的任务,该模板可以用作其他Bash程序的起点,然后探索了对其进行测试的方法。

本系列四篇文章中的第三篇介绍了如何创建和使用简单的帮助功能。 在创建帮助工具时,您还将了解如何使用函数以及如何处理-h等命令行选项。

为什么要帮助?

内置的帮助功能使您无需查看代码本身即可查看这些内容。 良好而完整的帮助工具也是程序文档的一部分。

关于功能

Shell函数是Bash程序语句的列表,这些语句存储在Shell的环境中,并且可以像其他任何命令一样通过在命令行中键入其名称来执行。 Shell函数也称为过程或子例程,具体取决于您使用的是哪种其他编程语言。

就像在其他任何命令中一样,在脚本中或从命令行界面(CLI)使用它们的名称来调用函数。 在CLI程序或脚本中,函数中的命令在被调用时执行,然后程序流序列返回到调用实体,并在该实体中执行下一个系列的程序语句。

函数的语法为:

 FunctionName ( ) { program statements } 

通过在CLI上创建一个简单的功能来探索这一点。 (该函数存储在创建它的shell实例的shell环境中。)您将创建一个名为hw的函数,该函数代表“ hello world”。 在CLI上输入以下代码,然后按Enter键 。 然后像输入其他任何shell命令一样输入hw


   
   
[ student @ testvm1 ~ ] $ hw ( ) { echo "Hi there kiddo" ; }
[ student @ testvm1 ~ ] $ hw
Hi there kiddo
[ student @ testvm1 ~ ] $

好的,所以我对标准的“ Hello world”启动器有些厌倦。 现在,列出所有当前定义的功能。 它们很多,因此我只展示新的硬件函数。 从命令行或程序中调用它时,函数执行其编程的任务,然后退出并将控制权返回给调用实体,命令行或脚本中调用语句之后的下一个Bash程序语句:


   
   
[ student @ testvm1 ~ ] $ declare -f | less
< snip >
hw ( )
{
    echo "Hi there kiddo"
}
< snip >

删除该功能,因为您不再需要它。 您可以使用unset命令执行此操作:


   
   
[ student @ testvm1 ~ ] $ unset -f hw ; hw
bash: hw: command not found
[ student @ testvm1 ~ ] $

创建帮助功能

在编辑器中打开hello程序,然后在版权声明之后但在回显“ Hello world!”之前将下面的“帮助”功能添加到hello程序代码中 声明。 此帮助功能将显示该程序的简短说明,语法图以及可用选项的简短说明。 添加对Help函数的调用以对其进行测试,并添加一些注释行,这些注释行提供了函数与程序主要部分之间的直观分界:


   
   
################################################################################
# Help                                                                         #
################################################################################
Help ( )
{
    # Display Help
    echo "Add description of the script functions here."
    echo
    echo "Syntax: scriptTemplate [-g|h|v|V]"
    echo "options:"
    echo "g     Print the GPL license notification."
    echo "h     Print this Help."
    echo "v     Verbose mode."
    echo "V     Print software version and exit."
    echo
}

################################################################################
################################################################################
# Main program                                                                 #
################################################################################
################################################################################

Help
echo "Hello world!"

此帮助功能中描述的选项是我编写的程序的典型选项,尽管代码中都没有。 运行程序进行测试:


   
   
[ student @ testvm1 ~ ] $ . / hello
Add description of the script functions here.

Syntax: scriptTemplate [ -g | h | v | V ]
options:
g     Print the GPL license notification.
h     Print this Help.
v     Verbose mode.
V     Print software version and exit.

Hello world !
[ student @ testvm1 ~ ] $

因为您没有添加任何逻辑以仅在需要时显示帮助,所以程序将始终显示帮助。 由于该功能正常运行,请继续阅读以添加一些逻辑,以仅在在命令行上调用程序时使用-h选项时显示“帮助”。

处理选项

Bash脚本具有处理-h等命令行选项的功能,可以提供一些强大的功能来引导程序和修改程序。 对于-h选项,您希望程序将帮助文本打印到终端会话,然后退出而不运行程序的其余部分。 可以使用while命令( 与如何使用Bash进行编程:循环以了解有关while的更多信息)结合getopscase命令将处理在命令行中输入的选项的功能添加到Bash脚本中。

getops命令读取命令行中指定的所有选项,并创建这些选项的列表。 在下面的代码中, while命令通过为每个选项设置变量$ options来遍历选项列表。 case语句用于依次评估每个选项,并在相应的节中执行这些语句。 while语句将继续评估选项列表,直到所有选项都已被处理,或者遇到退出语句,从而终止程序。

确保在回显“ Hello world!”之前删除帮助功能调用 语句,这样程序的主体现在看起来像这样:


   
   
################################################################################
################################################################################
# Main program                                                                 #
################################################################################
################################################################################
################################################################################
# Process the input options. Add options as needed.                            #
################################################################################
# Get the options
while getopts ":h" option; do
    case $option in
      h ) # display Help
         Help
          exit ;;
    esac
done

echo "Hello world!"

请注意-h的case选项中exit语句末尾的双分号。 这是添加到此case语句中以描述每个选项结尾的每个选项所必需的。

测试中

现在测试要复杂一些。 您需要使用许多不同的选项(没有选项)来测试程序,以查看程序如何响应。 首先,不进行任何测试以确保其显示“ Hello world!”。 正如它应该:


   
   
[ student @ testvm1 ~ ] $ . / hello
Hello world !

那行得通,所以现在测试显示帮助文本的逻辑:


   
   
[ student @ testvm1 ~ ] $ . / hello -h
Add description of the script functions here.

Syntax: scriptTemplate [ -g | h | t | v | V ]
options:
g     Print the GPL license notification.
h     Print this Help.
v     Verbose mode.
V     Print software version and exit.

这可以按预期工作,因此请尝试进行一些测试,以查看输入一些意外选项时会发生什么:


   
   
[ student @ testvm1 ~ ] $ . / hello -x
Hello world !
[ student @ testvm1 ~ ] $ . / hello -q
Hello world !
[ student @ testvm1 ~ ] $ . / hello -lkjsahdf
Add description of the script functions here.

Syntax: scriptTemplate [ -g | h | t | v | V ]
options:
g     Print the GPL license notification.
h     Print this Help.
v     Verbose mode.
V     Print software version and exit.

[ student @ testvm1 ~ ] $

该程序将忽略任何选项,而无需特定的响应,而不会产生任何错误。 但是请注意最后一个条目(选项-lkjsahdf ):因为选项列表中有一个h ,所以程序会识别它并打印帮助文本。 该测试表明该程序没有能力处理错误的输入并在检测到任何错误时将其终止。

您可以在case语句中添加另一个case节,以匹配没有明确匹配的任何选项。 此一般情况将匹配您未提供特定匹配项的任何内容。 现在,case语句类似于\? 作为最后一种情况。 任何其他特定情况都必须在此最后一个情况之前:


   
   
while getopts ":h" option; do
    case $option in
      h ) # display Help
         Help
          exit ;;
     \? ) # incorrect option
          echo "Error: Invalid option"
          exit ;;
    esac
done

使用与以前相同的选项再次测试该程序,然后看它现在如何工作。

你在哪

通过添加处理命令行选项和帮助过程的功能,您已经在本文中取得了很多成就。 现在,您的Bash脚本如下所示:


   
   
#!/usr/bin/bash
################################################################################
#                              scriptTemplate                                  #
#                                                                              #
# Use this template as the beginning of a new program. Place a short           #
# description of the script here.                                              #
#                                                                              #
# Change History                                                               #
# 11/11/2019  David Both    Original code. This is a template for creating     #
#                           new Bash shell scripts.                            #
#                           Add new history entries as needed.                 #
#                                                                              #
#                                                                              #
################################################################################
################################################################################
################################################################################
#                                                                              #
#  Copyright (C) 2007, 2019 David Both                                         #
#  LinuxGeek46@both.org                                                        #
#                                                                              #
#  This program is free software; you can redistribute it and/or modify        #
#  it under the terms of the GNU General Public License as published by        #
#  the Free Software Foundation; either version 2 of the License, or           #
#  (at your option) any later version.                                         #
#                                                                              #
#  This program is distributed in the hope that it will be useful,             #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
#  GNU General Public License for more details.                                #
#                                                                              #
#  You should have received a copy of the GNU General Public License           #
#  along with this program; if not, write to the Free Software                 #
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   #
#                                                                              #
################################################################################
################################################################################
################################################################################

################################################################################
# Help                                                                         #
################################################################################
Help ( )
{
    # Display Help
    echo "Add description of the script functions here."
    echo
    echo "Syntax: scriptTemplate [-g|h|t|v|V]"
    echo "options:"
    echo "g     Print the GPL license notification."
    echo "h     Print this Help."
    echo "v     Verbose mode."
    echo "V     Print software version and exit."
    echo
}

################################################################################
################################################################################
# Main program                                                                 #
################################################################################
################################################################################
################################################################################
# Process the input options. Add options as needed.                            #
################################################################################
# Get the options
while getopts ":h" option; do
    case $option in
      h ) # display Help
         Help
          exit ;;
     \? ) # incorrect option
          echo "Error: Invalid option"
          exit ;;
    esac
done

echo "Hello world!"

确保非常彻底地测试该版本的程序。 使用随机输入,看看会发生什么。 您还应该尝试测试有效和无效的选项,而不要使用前面的破折号( - )。

下次

在本文中,您添加了“帮助”功能以及处理命令行选项以有选择地显示它的功能。 该程序变得越来越复杂,因此测试变得越来越重要,并且需要更多测试路径才能完成。

下一篇文章将着眼于初始化变量并进行一些完整性检查,以确保程序将在正确的条件下运行。

资源资源


本系列文章部分基于David Both的Linux自学课程(共三部分)的第2卷第10章,该课程使用和管理Linux —从零到SysAdmin

翻译自: https://opensource.com/article/19/12/help-bash-program

bash 后台程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值