2

*** ADDENDUM: I use Windows10, GNUWIN32 and Cygwin64 ***

Suppose I have the following screen output:

command1 
command2 
command3

The desired result is each line shown above is executed as a command immediately after it is displayed on screen.

command1
command2
command2

I have read this post which explains how to insert "echo" before each line of screen output. How to apply shell command to each line of a command output? It does this:

ls -1 | xargs -L1 echo

What I have done for the time being is insert "run.bat" infront of each line. The result is the following lines are executed:

run.bat command1
run.bat command2
run.bat command3

The content of run.bat is this:

%1 %2 %3 %4 %5 %6 %7 %8 %9

It works. But this work-around seems unncessarily long.

How do I use xarg (or awk or sed) to take each line of screen output as an individual command, and run it immediately and exactly as it is displayed on screen please?

PS: I also do not want to redirect & save all screen output into a separate batchfile first, and then execute all lines that way. I would like each line of screen output to be execute exactly as a command, exactly as it is displayed and immediately after the line is displayed (I do not need to add any prefix or suffixed to the lines; just execute each line exactly as displayed on screen).

Thanks.

George
  • 121
  • 7
  • What shell are the commands written in? The use of `awk` and `xargs` suggests it is probably bash, but the name `run.bat` and the syntax `%1 %2` etc suggest windows batch file. Please edit your question to clarify the language of the commands. – user000001 Aug 23 '22 at 08:27
  • 1
    Hi user0000001, I use Windows10, GNUWIN32 and Cygwin64. The shell is cmd.exe under Windows 10. – George Aug 23 '22 at 11:22
  • If you're using cygwin then the shell should be bash, not cmd.exe. Please verify if you're using bash or cmd.exe. – Ed Morton Aug 23 '22 at 12:29
  • If you're really trying to call a Unix command provided by Cygwin from a Windows "batch" file (or `cmd.exe`?), maybe https://stackoverflow.com/questions/58878922/gawk-3-1-6-1-on-windows-7-x64-pro-gets-0-return-code-using-system-even-on-fail/58879683#58879683 will give you some ideas but just calling the Unix tools from a bash shell started by cygwin.exe would obviously be simpler. – Ed Morton Aug 23 '22 at 12:32
  • You may be right Ed. The shell should be bash if I'm using GNUWIN/Cygwin. I tested out this command: C:\Windows\system32>dir | c:\gnuwin32\bin\xargs -n1 bash -c 'echo "$@"; "$@"' and this: C:\Windows\system32>dir | c:\cygwin64\bin\xargs -n1 bash -c 'echo "$@"; "$@"' They produced this error message respectively: c:\gnuwin32\bin\xargs: bash: No such file or directory c:\cygwin64\bin\xargs: bash: No such file or directory The expected error is the following: XXXXX is not recognized as an internal or external command, operable program or batch file. Where XXXXX is each line of dir – George Aug 24 '22 at 18:45

1 Answers1

2

You can pipe your output to this xargs command line:

... | xargs -n1 bash -c 'echo "$@"; "$@"' _

This xargs calles bash -c script that echoes each line on stdout and then executes it.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Yes I answered this question too with something similar, but then I realized that OP is probably using windows batch, and not `bash`. – user000001 Aug 23 '22 at 08:28