我必须在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
我试过猛拉但是一次复制1行,有没有办法复制所有的行?
答案 0 :(得分:1)
正则表达式是你需要的:
:%s/^\(nohup\) \([a-zA-Z0-9]*\)/echo "Running \2"; \1 \2/
答案 1 :(得分: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次宏并格式化文件。
答案 2 :(得分:0)
为什么不使用sh set -x?
来自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.