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的更多信息)结合getops和case命令将处理在命令行中输入的选项的功能添加到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!"
确保非常彻底地测试该版本的程序。 使用随机输入,看看会发生什么。 您还应该尝试测试有效和无效的选项,而不要使用前面的破折号( - )。
下次
在本文中,您添加了“帮助”功能以及处理命令行选项以有选择地显示它的功能。 该程序变得越来越复杂,因此测试变得越来越重要,并且需要更多测试路径才能完成。
下一篇文章将着眼于初始化变量并进行一些完整性检查,以确保程序将在正确的条件下运行。
资源资源
如何使用Bash编程:语法和工具
如何使用Bash编程:逻辑运算符和Shell扩展
如何使用Bash编程:循环
本系列文章部分基于David Both的Linux自学课程(共三部分)的第2卷第10章,该课程使用和管理Linux —从零到SysAdmin 。
翻译自: https://opensource.com/article/19/12/help-bash-program
bash 后台程序
bash 后台程序_如何向您的Bash程序添加帮助工具相关推荐
- 11无监听程序_腾讯开心鼠英语 小程序实践与总结
腾讯开心鼠英语 团队中有很多小程序的项目,且后续还会很多小程序的开发和迭代规划,因此我们团队是小程序的重度使用者.在小程序的开发中,团队积累了一些技术和经验,也遇到了一些困难和挑战,还踩了很多坑,因此 ...
- plsql developer无监听程序_腾讯开心鼠英语 小程序实践与总结
腾讯开心鼠英语 团队中有很多小程序的项目,且后续还会很多小程序的开发和迭代规划,因此我们团队是小程序的重度使用者.在小程序的开发中,团队积累了一些技术和经验,也遇到了一些困难和挑战,还踩了很多坑,因此 ...
- 您没有权限来打开应用程序_苹果建议:除非应用程序无响应,否则不要滑动强制退出...
紫金财经2月26日消息 今日,苹果公司发布的一条建议,成为了微博热搜的话题.苹果建议除非应用程序无响应,否则不要滑动强制退出. 苹果公司表示,滑动关闭iPhone的应用程序可能会缩短电池寿命,并使设备 ...
- 渐进式web应用程序_如何使用渐进式Web应用程序更快,更便宜地构建新应用程序...
渐进式web应用程序 You need an app! The question is- which kind? 您需要一个应用程序! 问题是--哪种? For the last 8 years or ...
- 渐进式web应用程序_如何在渐进式Web应用程序中添加到主屏幕
渐进式web应用程序 添加到主屏幕 (Add To Homescreen) Here the web app install banner is focused on web app, with th ...
- 一个完整的嵌入式程序_放下偏见,原来嵌入式程序员如此“妖娆”!
感兴趣的小伙伴可以来我的Java交流群,可以获取免费的学习资料 828 697 593 对Java技术,架构技术感兴趣的同学,欢迎加群,一起学习,相互讨论. 竟然都看到最后了,给小编点个关注吧,小编还 ...
- java俄罗斯方块程序_使用JAVA编写的俄罗斯方块程序, 具有非常全面的功能.
Lastsong-Tetris 使用JAVA编写的俄罗斯方块程序, 具有非常全面的功能. 游戏基本规则: 1.打开游戏窗口后, 点击开始按钮进行游戏; 2.每消一行就会增加10分和1消行数,当分数累计 ...
- windows桌面应用程序_如何将Windows桌面应用程序转换为通用Windows应用程序
windows桌面应用程序 With Windows 10's Anniversary Update, Microsoft is making it possible for developers t ...
- object picker 微信小程序_七夕地图导航微信小程序
七夕期间,开发一款七夕微信小程序,地图导航是七夕小程序一种功能,今天单独开发一款纯七夕地图导航小程序,供大家娱乐,希望大家喜欢. 准备着手实现一个小程序,功能包括--获取用户当前位置的经纬度,在地图上 ...
最新文章
- 再见,FastJson...
- 综述:如何给模型加入先验知识
- LINUX使用tcgetattr函数与tcsetattr函数控制终端二
- [C#] 简单的 Helper 封装 -- RegularExpressionHelper
- springmvc rest风格化案例
- hdu - 2667 Proving Equivalences(强连通)
- error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/r
- azure上传代码_深入了解Azure Data Studio:更多代码和更少GUI
- 简明Python3教程 15.异常
- Jquery—Jquery中的(function($){...})(jQuery)
- [目标跟踪] 论文笔记:Parallel Tracking and Verifying(PTAV-Update)
- APPstore上架问题 ERROR ITMS-90096
- 学会一招!如何利用 pandas 批量合并 Excel?
- html5游戏编程核心技术与实战目录
- 'npm'不是内部命令或外部命令的解决办法
- 灰色预测模型MATLAB代码及使用
- 培训机构管理信息系统设计与开发(一)
- Python库pillow(PIL)的使用
- macOS和Linux下source和export命令
- YOLOv7半自动标注