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

dan*_*els 3 linux bash shell

假设我们有一个类似下面的bash脚本:

echo test
ls -alh
pwd
echo test2
Run Code Online (Sandbox Code Playgroud)

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

然后上面的文件像这样运行/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
Run Code Online (Sandbox Code Playgroud)

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

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

+ echo test
test
Run Code Online (Sandbox Code Playgroud)

另一个

+ 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
Run Code Online (Sandbox Code Playgroud)

等等

我正在考虑解析输出并查看行是否以该行开头,+然后将其作为一个命令的开头,但随后您可以很容易地得到类似的东西echo + ok会使该逻辑失败.

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

有任何想法吗?

tha*_*guy 7

+不是硬编码,并且这在容易描述man bash和下help set-x:

  -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.
Run Code Online (Sandbox Code Playgroud)

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

   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 ``+ ''.
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

$ PS4=$'\nAnd now, a word from '; set -x; date; uptime

And now, a word from date
Mon Apr  3 16:20:35 PDT 2017

And now, a word from uptime
 16:20:35 up 65 days,  1:24,  6 users,  load average: 1.20, 1.42, 1.37
Run Code Online (Sandbox Code Playgroud)

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