加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 23:53:21 所属栏目:Linux 来源:网络整理
导读:假设我们有一个类似下面的bash脚本: echo testls -alhpwdecho test2 因此,文件上可以有任意数量的命令,每个命令产生或不产生自己的输出. 然后上面的文件就像这个/ bin / bash -xe test.sh一样运行,它将产生以下输出: + echo testtest+ ls -alhtotal 32drwx
假设我们有一个类似下面的bash脚本:

echo test
ls -alh
pwd
echo test2

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

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

+ echo test
test
+ ls -alh
total 32
drwx------+  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_Store
drwxr-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 test2
test2

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

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

+ echo test
test

另一个

+ ls -alh
total 32
drwx------+  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_Store
drwxr-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

等等

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

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

有任何想法吗?

解决方法

不是硬编码的,这很容易在man bash和-x的帮助集中描述:

-x      After expanding each simple command,for  command,case  command,select  command,or arithmetic for
          command,display the expanded value of  PS4,fol                        

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读