Shell 脚本和自动化指南

发布于 2024-11-05 13:10:20 字数 287 浏览 0 评论 0原文

我目前正在开发一些脚本,这些脚本可以自动化很多东西并且相当中等大小。我已经开始遵循某些事情来使我的脚本通用和可移植,例如:

  • 管理员登录用于调度脚本
  • 使用绝对路径
  • 使用配置文件
  • 在开发时使用写入主机并在部署(调试)时切换到日志记录

我最感兴趣了解是否有任何标准脚本技术和指南旨在使脚本开发人员的生活变得简单。我同时使用 perl 和 powershell,因此非常欢迎和赞赏更通用的建议。

感谢您提前提供的任何帮助。
钢铁用户

I am currently working on some scripts that automate a lot of stuff and are fairly medium sized. I have started to follow certain things to make my scripts generic and portable like:

  • administrator login for scheduling scripts
  • use of absolute paths
  • use of config files
  • use write-host when developing and switch to logging when deploying (debugging)

I would be most interested in knowing if there are any standard scripting techniques and guidelines out there that aim to make a script developers life simple. I work on both perl and powershell, so a more generalized advice would be most welcome and appreciated.

Thanks for any help in advance.
steeluser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-11-12 13:10:20

这有点宽泛,我确信这是主观的。不过,我已经完成了一些脚本工作,并且可以提供一些我从经验中学到的东西。

  1. 让您的脚本数据驱动。硬编码尽可能少的东西。所有诸如路径、资源位置等的内容都应该可以从配置文件中更改,而不是通过编辑主脚本来更改。这与您关于绝对路径的观点相关。对所有路径进行硬编码是不行的。让一切变得相对会带来太多的限制。最好有一个名为 DATA_ROOT 的变量,您可以修改该变量,然后让所有例程引用诸如 ${DATA_ROOT}/resources/images 之类的内容> 等等。 ${PROGRAM_ROOT} 也很有用,这样您就可以安装多个套件来测试等。
  2. 错误检查。脚本语言通常是动态类型的,因此请确保检查所做的每件事的返回值。创建一个库,您可以使用它执行诸如 call_safely(fn, arg0, arg1,.. ) 和 call_and_abort_on_failure(fn, arg0, arg1,...) 之类的事情甚至可能是有意义的。
  3. 自由地评论和记录。脚本更容易修改已编译的程序,因此当人们想要扩展它们时,它们经常以错误的方式进行修补。确保您的 API 有完整的文档记录,以便当人们想要添加新的命令行选项时,他们知道如何操作,而不是在您的 API 之上编写包装脚本。
  4. 让一个脚本做一件事,如果需要的话可以编写聚合器。具有 if(FLAG) {do_something} else {do_something_completely_other} 的脚本会杀死你。让您的脚本做一件事并做好它,如果您愿意,您可以根据需要编写委托给这些脚本的包装器(以获得更好的用户体验)。
  5. 自由使用第三方工具,而不是发明自己的工具。命令行解析、文本处理等都是老领域,有很多库可以很好地完成这些任务。使用它们而不是重新实现。 最无错误的程序是从未编写过的程序。
  6. 让您的日志记录和测试框架坚如磐石。当糟糕的事情发生时,您应该知道一切,而不必重新运行脚本。从第一天开始就使用一些标准的日志记录框架,并使其可通过可按需重新加载的文件进行配置。确保您的代码经过单元测试,以便在进行新更改时不会引入回归错误。另外,记录计时指标,以便您随着时间的推移了解脚本在哪里变慢。将来会有用的。
  7. 让您的脚本尽可能免于修改。这很难描述,但我可以给你举个例子。我有一些脚本来跟踪各个分支的构建的各种指标并将它们放入一个目录中。我编写它的目的是,如果您想要跟踪分支 foo,您所要做的就是在某个位置创建一个名为 foo 的目录,它就会跟踪该目录。您不必修改很多东西,真正想要跟踪指标的经理可以根据需要创建目录。

这些是我从头到尾能想到的几点。

This is kind of broad and I'm sure subjective. However, I've done some scripting work and can offer some things I've learnt from experience.

  1. Make your scripts data driven. Hardcode as few things as possible. All things like paths, resource locations etc. should be changeable from a config file rather than by editing the main script. This ties in with your point about absolute paths. Hardcoding all the paths is a no no. Making everything relative imposes too many constraints. It would be nice to have a single variable called, for example, DATA_ROOT which you can modify and then have all the routines refer to things like ${DATA_ROOT}/resources/images etc. A ${PROGRAM_ROOT} would be useful too so that you can have multiple installations of your suite to test etc.
  2. Error checking. Scripting languages are often dynamically typed so make sure you check the return value of every thing you do. It might even make sense to make a library with which you can do things like call_safely(fn, arg0, arg1,.. ) and call_and_abort_on_failure(fn, arg0, arg1,...).
  3. Comment and document liberally. Scripts are easier to modify that compiled programs and so they're often patched in the wrong ways when people want to extend them. Make sure that your APIs are well documented so that when people want to, for example, add a new command line option, they know how to do it rather than write a wrapper script on top of yours.
  4. Make one script do one thing and write aggregators if you need. scripts that have if(FLAG) {do_something} else {do_something_completely_different} will kill you. Make your scripts do one thing and do it well and if you want, you can write wrappers that delegate to these scripts as necessary (for a better user experience).
  5. Use 3rd party tools liberally rather than invent your own. Command line parsing, text processing etc. are all old areas and there are lots of libraries that do them well. Use those rather than reimplement. The most bug free program is the one that's never written.
  6. Make your logging and testing frameworks rock solid. When bad things happen, you should know everything without having to rerun the scripts. Use some standard logging framework from day one and make it configurable from a file which can be reloaded on demand. Make sure that your code is unit tested so that when new changes are made, regressions bugs are not introduced. Also, log timing metrics so that you know over time where your scripts are slowing down. It will be useful in the future.
  7. Make your scripts as tinker free as possible. This is hard to describe but I can give you an example. I had some scripts to track various metrics on builds for various branches and drop them into a directory. I wrote it so that if you wanted a branch foo tracked, all you had to do was to create a directory called foo in a certain place and it would track that. You didn't have to tinker with a lot of stuff and the managers who actually wanted to track metrics could create the directories as they needed it.

These are the points I can think of from the top of my head.

最好是你 2024-11-12 13:10:20

只是一些我认为好的做法:

  • 只要您在运行时捕获它们,使用绝对路径就可以了。请参阅此问题
  • 对于配置文件,使用一些易于处理的格式,例如 xmlyaml
  • 不要使用 write-host,使用更好的 write-output< code>write-log 或者,更好的是,实现一个自定义函数,该函数允许您根据配置文件上的设置在控制台/日志之间切换。有关更多详细信息,请参阅我对此问题的回答。

这些只是根据您的观点提出的一些建议。


编辑 关于采用 XML 配置文件的注意事项

使用 XML,因为您的配置文件将包含一些设置,并且您将仅通过脚本对其进行管理。


希望这有帮助

干杯

just a few practices which I consider good:

  • Using absolute paths is ok as far as you catch them at runtime. See this question.
  • Use for configuration files some easily processing format such as xml or yaml.
  • DO NOT use write-host, use better write-output or write-log or, better, implement a custom function which allows you to switch between console/log according to a setting on your configuration files. See my answer to this question for any more details.

These are just a few suggestions based on your points.


EDIT Note about adopting XML configuration file

Use XML as far as your configuration file will contain a few settings and you are going to manage it only via script.


Hope this help

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文