1

I have to run a bunch of commands in a bash script and I want to echo which command is currently running:

I have this file (which is about 100 lines):

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

I would like:

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

I tried yank but it copy 1 line at a time, is there a way to copy all of the lines?

Erik
  • 79
  • 1
  • 1
  • 10

3 Answers3

1

regex is what you need here:

:%s/^\(nohup\) \([a-zA-Z0-9]*\)/echo "Running \2"; \1 \2/
xvedar
  • 26
  • 3
  • please go to :help subs :[range]s/{pattern}/{string}/ pattern: ^ - beginning of the line \(nohup\) - is \1 [a-zA-Z0-9]* - all listed characters 0 or more times \([a-zA-Z0-9]*\) - is \2 – xvedar Jul 07 '14 at 14:46
0

You could record a macro.

Assuming you are on the first character of your file, you should type:

`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

If this work as expected, typing 98@@should recall the last macro 98 times and format your file.

Xavier T.
  • 40,509
  • 10
  • 68
  • 97
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.
emil
  • 1,642
  • 13
  • 12