在 IF ELIF 语句中执行一系列外部/第三方程序
我的脚本中有一个很长的 IF ELIF 语句,它可以工作。当我开始尝试让它执行外部程序/第三方软件时,我的问题就开始了。
.....; then
prog1 -some flags
prog2 -some flags
prog3 -some more flags
prog4 -even more flags
elif [......]
正如你可以猜到的,我发现这是不收费的,我能看到的唯一解决方法是我将这些命令放入一个单独的 shell 脚本中并按如下方式调用它:
.....; then
./myshellscript.sh
elif [......]
是否可以将其包含在脚本自身中而不需要必须调用 shell 脚本来完成我的所有任务。我通常会硬着头皮这样做,但这意味着有数百个外部 shell 脚本,这些脚本会使文件夹变得混乱,而不是有一个包含执行手头任务所需的所有代码的最终脚本。
再次,任何帮助将不胜感激。
My script has a prety long IF ELIF statement in it which works. My problems started when I began trying to get it to execute external programs/3rd party software.
.....; then
prog1 -some flags
prog2 -some flags
prog3 -some more flags
prog4 -even more flags
elif [......]
As you can gues I have found out that this is not feesable, the only work around I can see is that I put these commands into a seperate shell script and call it as below:
.....; then
./myshellscript.sh
elif [......]
Is there away of containing this in the script its self without having to call shell scripts to do all my tasks. I would normally bite the bullet and do this but that would mean having hundreds of external shell scripts which would be cluttering up the folder rather than having one deffinitive script containing all the code that is required to carry out the task at hand.
Once again any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只关心每个分支中调用脚本的数量,并且每个分支中调用的脚本是相同的,那么您可以只使用 bash 函数来完成这项工作。
查看此页面,了解如何编写函数在bash中。
这基本上与将公共代码分解为几乎任何语言的函数的方式相同。
If you are just concerned about the number of calls to scripts in each branch, and if the scripts, which are called in each branch, are the same, then you could just use bash functions to do the work.
Have a look at this page, to see how to write functions in bash.
This is basically the same way, as the one in which you would factor out common code into functions in almost any language.