Shell 脚本和自动化指南
我目前正在开发一些脚本,这些脚本可以自动化很多东西并且相当中等大小。我已经开始遵循某些事情来使我的脚本通用和可移植,例如:
- 管理员登录用于调度脚本
- 使用绝对路径
- 使用配置文件
- 在开发时使用写入主机并在部署(调试)时切换到日志记录
我最感兴趣了解是否有任何标准脚本技术和指南旨在使脚本开发人员的生活变得简单。我同时使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点宽泛,我确信这是主观的。不过,我已经完成了一些脚本工作,并且可以提供一些我从经验中学到的东西。
DATA_ROOT
的变量,您可以修改该变量,然后让所有例程引用诸如${DATA_ROOT}/resources/images
之类的内容> 等等。 ${PROGRAM_ROOT} 也很有用,这样您就可以安装多个套件来测试等。if(FLAG) {do_something} else {do_something_completely_other}
的脚本会杀死你。让您的脚本做一件事并做好它,如果您愿意,您可以根据需要编写委托给这些脚本的包装器(以获得更好的用户体验)。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.
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.call_safely(fn, arg0, arg1,.. )
andcall_and_abort_on_failure(fn, arg0, arg1,...)
.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).foo
tracked, all you had to do was to create a directory calledfoo
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.
只是一些我认为好的做法:
write-host
,使用更好的write-output
或 < code>write-log 或者,更好的是,实现一个自定义函数,该函数允许您根据配置文件上的设置在控制台/日志之间切换。有关更多详细信息,请参阅我对此问题的回答。这些只是根据您的观点提出的一些建议。
编辑 关于采用 XML 配置文件的注意事项
使用 XML,因为您的配置文件将包含一些设置,并且您将仅通过脚本对其进行管理。
希望这有帮助
干杯
just a few practices which I consider good:
write-host
, use betterwrite-output
orwrite-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