batch script print the command that would be executed rather than executing - windows

Is it possible to set a cmd.exe shell / batch file to print what would be executed but not actually execute it?
For example, given a batch file that takes some arguments, based on those arguments selects some other batch files to run, those batch files execute some commands, may or may not call other files/commands etc.
I would like to be able to run the top level batch file with all possible combinations of it's input arguments and capture what each arg combination would execute - without actually trying to execute it.
e.g. conceptually would want to be able to produce something like:
mybatchfile.bat 1 2 3 > mybatchfile_1_2_3.bat
mybatchfile.bat 99 3 42 > mybatchfile_99_3_42.bat
where mybatchfile_99_3_42.bat is the list of everything that WOULD be executed when running mybatchfile.bat 99 3 42 (NOT the output of executing those commands)
If this can't be done solely using cmd.exe is there someway to achieve this by running the batch script in cygwin bash shell

In bash we would use something like -x to print out all possible commands without executing them. how to make bash scripts print out every command before executing The problem is that to my knowledge there's no exact equivalent command for Batch Scripts. I would suggest you try placing:
#echo on
at the beginning of your script and:
#echo off
at the end of your script, that's the best starting place.

If you never want the batch file to actually execute the commands, you can insert echo before each command. It's not a perfect solution by any means, but it may be a work-around for fairly simple scripts.

Related

Cannot run multiple commands in one line [duplicate]

This question already has answers here:
WinSCP script not executing in batch file
(2 answers)
Closed last year.
I am trying to run multiple commands in one line. The codes are below.
WinSCP.com
open user:pw#address
lcd C:\Users\xx\Desktop
get *.xlsx
exit
If I run them one by one, it will work. However, if I want to run them all together using one line, it will fail, no matter I am using ; or &.
For example, if I run WinSCP.com & open user:pw#address, only the first part will be executed.
WinSCP.com; open user:pw#address doesn't work either.
How to execute them using one line?
Thanks
This is happening, because both command line and winscp.com are programs. When you're entering commands one by one you start winscp.com and next entered commands are going directly to the WinSCP. When you're trying to execute all commands at once it is starting WinSCP and waiting for it to finish and then executing next commands.
You definitely can create a text file with your commands and save it as script.txt
open user:pw#address
lcd C:\Users\xx\Desktop
get *.xlsx
exit
And then pass it to the winscp.com application like this:
winscp.com /script=script.txt
It may be possible to send all the commands at once by using /command cmd1 cmd2 ... where cmd1,2 are your commands, but it might be tricky.
References
Command-line Options :: WinSCP
Parameters
You can put the sequence of commands in a script file used by WinSCP and call it using the command sequence:
WinSCP.com /script="C:\Users\{name}\Documents\Scripts\{SomeFileName}.scp"
I have found it easier to keep track of what you are doing and keeps the command sequence easier to read.
These are all the possible ways of running multiple commands on a single line :
When you use command1 & command2 on the command prompt, it
executes the first command, and then the second command.
command1 && command2 it runs the first command, and then runs the second command only if the first command completed successfully.
command1 || command2 it runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).
(command1 & command2) used to group or nest multiple commands.
Since in your case, you want to run the first command and then do the other operation with-in it; it isn't possible to use any of the above.
You have to write a script which you may call in the same line using a command.
You may do something like this : (WinSCP.com /script=<script_path>) && exit
There are many more options in WinSCP.com command.

Calling two functions in Windows batch file one after the other

I have two scripts which are to be executed and i am using Windows batch script for automating of running the scripts.
I have to read 10 user input values and then run the two scripts using those parameters.
I am successful in executing the first script and failing with the second script.
Issue is that the cmd prompt exits after completing the first script.
How to make cmd prompt run the second script as well using the input parameters.
Any Help on this??
Thanks in Advance.
You have two options to execute a batch script from you code. It's either START or CALL:
START will execute your code in it's own variable scope, means the variables you've set in the first script won't be available. Further both scripts will be executed in parallel and not one after another (unless you use START /WAIT).
CALL on the other hand will do exactly what you need. It will start the first script in the same scope (previously set variables are variable), execute it and afterwards it will run the second sript (also in the same scope).
TL;DR this will work:
...
CALL BatchScript1.bat
CALL BatchScript2.bat
...
If you call scripts from another script you have to use the call command:
#echo off
call first.cmd
call second.bat
echo Here I'm back again !

Basic Shell Script call command from another director with feedback

i'm trying to create a shell script so that it calls two commands in 2 seperate directories and then shows their feedback, To call the a command i'm guessing it would be something like this ./directory/ ./script.sh
Thanks in advance for your replies.
If you want to sequentially invoke the commands:
/path/to/command1; /path/to/command2
If you want to call the second command only if the first one succeeded:
/path/to/command1 && /path/to/command2
If you want to run them in parallel:
/path/to/command1 &
/path/to/command2
The output of the commands will be the standard output (most likely the terminal). If you run the two commands in parallel and they produce some output, you might want to redirect it to different files.

Stata command line arguments in batch mode

A helpful FAQ from Stata describes that arguments can be passed to do files. My do file looks like this:
* program.do : Program to fetch information from main dataset
args inname outname
save `outname', emptyok // file to hold results
insheet using `inname', comma clear names case
// a bunch of processing
save `outname', replace
According to the FAQ, this script can be run using do filename.csv result.dta. When I run this command from within Stata, everything works fine. The program is long, however, so I want to run it in batch mode. Stata has another FAQ about batch mode.
Combining the information from these webpages, I type the following at my Unix prompt:
$ nohup stata -b do program.do filename.csv result.dta &
Stata starts up, but it terminates with the following error:
. save `outname', emptyok // file to hold results
invalid file specification
r(198);
A little experimentation tells me that Stata is never receiving the two arguments when I run the program in batch mode. What is the solution to this problem? (i.e. how do you pass arguments to a do file when running it in batch mode?)
The thread below may be helpful:
http://www.stata.com/statalist/archive/2012-09/msg00609.html
In Windows, if my program Test.do is:
args a b
display "`a'"
display "`b'"
I can run it in batch mode in Windows by simply typing:
"c:\Stata13\stata.exe" /e do "c:\Scripts\Test.do" Test Script
And it will display (within Stata):
Test
Script
So I wonder whether the nohup is what's preventing your program from working.

Redirect bash output called from batch file to console

I have a batch file (build.bat) which calls a bash script (makelibs.sh). The bash script contains several commands which build 20 libraries from source.
If I run makelibs.sh from MSYS, I get continuous output. If I call it from the batch file, then I see the full output only at the end of every single command.
This makes it difficult to assess the current status of the process.
Is it possible to redirect the output of makelibs.sh in order to get a continuous feedback on the execution?
I have a batch file (build.bat) which calls a bash script (makelibs.sh)
I strongly advise against doing this. You are calling a script with a script, when you could simply open up Bash and put
makelibs.sh
However if you insist on doing this then perhaps start would work
start bash.exe makelibs.sh
ref

Resources