热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

shell编程zenity_如何在Linux上使用Zenity制作简单的图形Shell脚本

shell编程zenityZenityaddsgraphicalinterfacestoshellscriptswithasinglecommand.Shellscriptsare
shell编程zenity

shell编程zenity

Zenity adds graphical interfaces to shell scripts with a single command. Shell scripts are a great way to automate repetitive tasks, but they’re normally confined to the terminal — Zenity brings them out of the terminal and onto your desktop.

Zenity只需一个命令即可将图形界面添加到Shell脚本。 Shell脚本是自动执行重复任务的好方法,但通常只限于终端机-Zenity将它们带出终端机并带到您的桌面上。

We’ve given an introduction to shell scripting in the past. You don’t have to be a programmer to get started with shell scripts — they require little more than knowledge of Linux terminal commands.

过去,我们已经介绍了Shell脚本 。 您无需成为程序员即可开始使用Shell脚本-它们只需要了解Linux终端命令即可。

获得热情 (Getting Zenity)

Zenity comes with Ubuntu by default. If you use an Ubuntu derivative, Such as Kubuntu, you may have to install it manually with the following command:

Zenity默认随附于Ubuntu。 如果使用Ubuntu衍生产品(例如Kubuntu),则可能必须使用以下命令手动安装它:

sudo apt-get install zenity

sudo apt-get install zenity

Zenity is a part of GNOME, so it should already be included on Linux distributions that use the GNOME desktop. Check your package manager for the zenity package if you don’t have it.

Zenity是GNOME的一部分,因此它应该已经包含在使用GNOME桌面Linux发行版中。 如果您没有zenity软件包,请检查您的软件包管理器。

使用Zenity (Using Zenity)

You can play around with Zenity from the terminal. Let’s say you want to create an error window when a problem occurs with your shell script. Here’s an example command you could use:

您可以在终端上玩Zenity。 假设您要在Shell脚本出现问题时创建一个错误窗口。 这是您可以使用的示例命令:

zenity –error –title=”An Error Occurred” –text=”A problem occurred while running the shell script.”

zenity –error –title =“发生错误” –text =“运行shell脚本时发生问题。”

Run the command and you’ll see a window with the message.

运行命令,您将看到一个带有消息的窗口。

Put this single command into your shell script at the correct place and you’ll have a graphical error message. You could also use variables to include more information about the error.

将此命令放在正确的位置的Shell脚本中,您将收到图形错误消息。 您还可以使用变量来包含有关该错误的更多信息。

Let’s say you want to ask a yes or no question. You could use a command like this one:

假设您要询问是或否的问题。 您可以使用如下命令:

zenity –question –title=”Query” –text=”Would you like to run the script?”

zenity –问题–title =“查询” –text =“您要运行脚本吗?”

You can catch the yes or no response in your shell script and perform different commands based on which button the user clicks.

您可以在shell脚本中捕获是或否响应,并根据用户单击的按钮执行不同的命令。

There’s also a text entry dialog:

还有一个文本输入对话框:

zenity –entry –title=”Favorite Website” –text=”What is your favorite website?”

zenity –entry –title =“最喜欢的网站” –text =“您最喜欢的网站是什么?”

Catch the user’s input in a shell script and you could store it as a variable.

在shell脚本中捕获用户的输入,您可以将其存储为变量。

There’s also a file picker, calendar, and other types of dialogs. For a full list of dialog types and their options, consult Zenity’s manual page.

还有一个文件选择器,日历和其他类型的对话框。 有关对话框类型及其选项的完整列表,请参阅Zenity的手册页 。

示例脚本 (An Example Script)

Let’s try using Zenity to create a simple graphical shell script. With just three commands, we can create a graphical timer program:

让我们尝试使用Zenity创建一个简单的图形化shell脚本。 仅需三个命令,我们就可以创建一个图形计时器程序:

#!/bin/bash # This script asks the user for a time, waits the specified amount # of time, and shows an alert dialog.

#!/ bin / bash#此脚本询问用户一段时间,等待指定的时间#,并显示警报对话框。

TIME=$(zenity –entry –title=”Timer” –text=”Enter a duration for the timer.\n\n Use 5s for 5 seconds, 10m for 10 minutes, or 2h for 2 hours.”)

TIME = $(zenity –entry –title =“ Timer” –text =“输入计时器的持续时间。\ n \ n使用5s表示5秒,10m表示10分钟,或2h表示2小时。”)

sleep $TIME

睡$ TIME

zenity –info –title=”Timer Complete” –text=”The timer is over.\n\n It has been $TIME.”

zenity –info –title =“计时器已完成” –text =“计时器已结束。\ n \ n已经是$ TIME。”

We’re using some extra tricks here. We get the value of the TIME variable from the first zenity command and feed it to the sleep command. We’re also using /n to create new lines of text in the zenity dialogs.

我们在这里使用一些额外的技巧。 我们从第一个zenity命令获取TIME变量的值,并将其输入到sleep命令。 我们还使用/ n在zenity对话框中创建新的文本行。

After saving the shell script and running the chmod +x command on it to give it executable permissions, we can launch it.

保存shell脚本并对其运行chmod + x命令以赋予其可执行权限后,我们可以启动它。

Enter a duration and the script will use the standard sleep command to count down in the background. When the sleep command’s timer finishes, the script will display the zenity info message.

输入持续时间,脚本将使用标准的sleep命令在后台倒数。 当sleep命令的计时器结束时,脚本将显示“ zenity info”消息。

You could create a desktop or panel shortcut for this script and run it without even touching the terminal.

您可以为该脚本创建桌面或面板快捷方式,并在不触摸终端的情况下运行它。



This is just scratching the surface of what you could do with zenity; you could use it to make much more complicated programs. If you’re looking for more information on shell scripting, check out our guide to using for loops in shell scripts.

这只是在摸索您可以使用Zenity做的事情的表面; 您可以使用它来制作更复杂的程序。 如果您正在寻找有关shell脚本的更多信息,请查看我们的shell脚本中使用for循环的指南。

翻译自: https://www.howtogeek.com/107537/how-to-make-simple-graphical-shell-scripts-with-zenity-on-linux/

shell编程zenity



推荐阅读
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了Linux系统中正则表达式的基础知识,包括正则表达式的简介、字符分类、普通字符和元字符的区别,以及在学习过程中需要注意的事项。同时提醒读者要注意正则表达式与通配符的区别,并给出了使用正则表达式时的一些建议。本文适合初学者了解Linux系统中的正则表达式,并提供了学习的参考资料。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 在CentOS/RHEL 7/6,Fedora 27/26/25上安装JAVA 9的步骤和方法
    本文介绍了在CentOS/RHEL 7/6,Fedora 27/26/25上安装JAVA 9的详细步骤和方法。首先需要下载最新的Java SE Development Kit 9发行版,然后按照给出的Shell命令行方式进行安装。详细的步骤和方法请参考正文内容。 ... [详细]
  • Ubuntu安装常用软件详细步骤
    目录1.GoogleChrome浏览器2.搜狗拼音输入法3.Pycharm4.Clion5.其他软件1.GoogleChrome浏览器通过直接下载安装GoogleChro ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Ubuntu 9.04中安装谷歌Chromium浏览器及使用体验[图文]
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
author-avatar
HikariFocus_695
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有