linux – 如何为bash文件生成的每个命令分隔输出?

linux – 如何为bash文件生成的每个命令分隔输出?,第1张

概述假设我们有一个类似下面的bash脚本: echo testls -alhpwdecho test2 因此,文件上可以有任意数量的命令,每个命令产生或不产生自己的输出. 然后上面的文件就像这个/ bin / bash -xe test.sh一样运行,它将产生以下输出: + echo testtest+ ls -alhtotal 32drwx------+ 6 daniels sta 假设我们有一个类似下面的bash脚本:

@H_404_8@

@H_404_8@

echo testls -alhpwdecho test2

因此,文件上可以有任意数量的命令,每个命令产生或不产生自己的输出.@H_404_8@

然后上面的文件就像这个/ bin / bash -xe test.sh一样运行,它将产生以下输出:@H_404_8@

@H_404_8@

+ echo testtest+ ls -alhtotal 32drwx------+  6 danIEls  staff   204B Apr  3 23:33 .drwxr-xr-x+ 64 danIEls  staff   2.1K Apr  4 01:53 ..-rw-r--r--@  1 danIEls  staff   6.0K Apr  3 23:33 .DS_Storedrwxr-xr-x   5 danIEls  staff   170B Mar 15 17:03 Todo-rw-r--r--@  1 danIEls  staff   282B Apr  3 20:39 test.py-rw-r--r--@  1 danIEls  staff    97B Apr  4 01:52 test.sh+ pwd/Users/danIEls/Desktop+ echo test2test2

有没有办法解析生成的输出可靠,并找出如何根据每个命令分离输出?@H_404_8@

对于上面的示例,我们应该能够分离并提取一个组:@H_404_8@

@H_404_8@

+ echo testtest

另一个@H_404_8@

@H_404_8@

+ ls -alhtotal 32drwx------+  6 danIEls  staff   204B Apr  3 23:33 .drwxr-xr-x+ 64 danIEls  staff   2.1K Apr  4 01:53 ..-rw-r--r--@  1 danIEls  staff   6.0K Apr  3 23:33 .DS_Storedrwxr-xr-x   5 danIEls  staff   170B Mar 15 17:03 Todo-rw-r--r--@  1 danIEls  staff   282B Apr  3 20:39 test.py-rw-r--r--@  1 danIEls  staff    97B Apr  4 01:52 test.sh

等等@H_404_8@

我正在考虑解析输出并查看行是否以该行开始,然后将其作为一个命令的开头,然后您可以轻松地使用echo ok这样的 *** 作会使该逻辑失败.@H_404_8@

如果我们可以修改/ bin / bash -x输出的字符,那么另一种选择就是这样,而不是输出像https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text这样的东西,但看起来像是在bash中硬编码而不是可配置的.@H_404_8@

有任何想法吗?@H_404_8@解决方法 不是硬编码的,这很容易在man bash和-x的帮助集中描述:

@H_404_8@

@H_404_8@

-x      After expanding each simple command,for  command,case  command,select  command,or arithmetic for          command,display the expanded value of  PS4,fol‐          lowed by the command and its expanded arguments or          associated word List.

这里是PS4的进一步描述,也来自man bash:@H_404_8@

@H_404_8@

PS4    The value of this parameter is expanded as  with  PS1  and          the  value  is  printed  before each command bash displays          during an execution trace.  The first character of PS4  is          replicated  multiple times,as necessary,to indicate mul‐          tiple levels of indirection.  The default is ``+ ''.

这是一个例子:@H_404_8@

@H_404_8@

$PS4=$'\nAnd Now,a word from '; set -x; date; uptimeAnd Now,a word from dateMon Apr  3 16:20:35 PDT 2017And Now,a word from uptime 16:20:35 up 65 days,1:24,6 users,load average: 1.20,1.42,1.37

你可以使用它来嵌入你认为合适的特殊标记或字符.@H_404_8@ 总结

以上是内存溢出为你收集整理的linux – 如何为bash文件生成的每个命令分隔输出?全部内容,希望文章能够帮你解决linux – 如何为bash文件生成的每个命令分隔输出?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/1021267.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存