1

我必须在 bash 脚本中运行一堆命令,并且我想回显当前正在运行的命令:

我有这个文件(大约 100 行):

nohup command1 param1 param2
nohup command2 p1 p2
nohup cmd3 p1 p2 param3

我想:

echo "Running command1" ; nohup command1 param1 param2
echo "Running command2" ; nohup command2 p1 p2
echo "Running cmd3" ; nohup cmd3 p1 p2 param3

我试过 yank 但它一次复制 1 行,有没有办法复制所有行?

4

3 回答 3

1

正则表达式是您在这里需要的:

:%s/^\(nohup\) \([a-zA-Z0-9]*\)/echo "Running \2"; \1 \2/
于 2014-07-07T14:16:29.843 回答
0

Why not just use sh set -x?

From bash(1):

          -x      After expanding each simple command, for  command,  case
                  command, select command, or arithmetic for command, dis-
                  play the expanded value of PS4, followed by the  command
                  and its expanded arguments or associated word list.
于 2014-07-07T14:01:27.313 回答
0

你可以录制一个宏。

假设您在文件的第一个字符上,您应该键入:

`qa`              "Start recording a macro into a
`w`               "move to first word following nohup
`yiw`             "copy the word under cursor
`^`               "move cursor back to beginning of line
`i`               " enter insert mode
`echo "Runnning<ESC>pi";<ESC>`  "paste the previously copied word
`j`               " go down one line
`^`               "move cursor back to beginning of line
`q`               " stop recording the macro
`@a`              " rexecute macro on second line

如果这按预期工作,键入98@@应该调用最后一个宏 98 次并格式化您的文件。

于 2014-07-07T13:57:24.800 回答