Is there a command in Shell scripting for executing .exe file and running commands automatically inside of it? replacing the user interaction - windows

I have a .sh script file that I'm modifying which runs an .EXE file that opens the Windows command line prompt automatically.
This .exe asks the user for an input (name of the file in the folder workspace that it will read)
I want to automate this step in my shell script so my user doesn't have to interact with this, and run the commands automatically
I read a bit about the expect command but I think that is for Linux only.
Can someone help me, I'm pretty new to Shell scripting and I couldn't find any useful information elsewhere.

I'm assuming that your executable accepts command-line arguments. So, here we go.
You can use the "start" command in Windows Shell. For example:
start C:\path\to\program.exe -argument
If you want to make the script wait until the .exe file finishes running before continuing, you can use the "/wait" command:
start /wait C:\path\to\program.exe -argument
IF all of that doesn't work, please try:
start myprogram.exe /command1 /command2 /command3
Hope it helps,

Related

Using Batch File to Open Another Cmd Prompt and Run Cmd in that Cmd Prompt

I have another bat file that I'm running, and once in the command prompt that bat file creates, I want to run another command in that window.
Here's what I have so far:
call C:\Batch\MyBatFile.bat (this creates the new command prompt that I want to use)
C:\Program\MyProgram.exe
However, the second line is being run in the original window, instead of the new command prompt. I tried using start C:\Program\MyProgram.exe, but that just ran in a 3rd new window instead.
If it's relevant, the first line is just setting a few environment variables that I need access to and MyProgram is a visual studio 2010 project. Technically, I might be able to modify that bat to run the command, but I'd rather avoid that solution as that bat file isn't owned by me (and thus whenever it's updated I'd have to update mine as well).
Thanks in advance.
You could try to inject your program.exe into cmd created by batfile.bat by redirecting it's input stream and then sending it a command, eg. echo C:\Program\MyProgram.exe | C:\Batch\MyBatFile.bat. This assumes that batch really just sets bunch of variables and does not use commands which reset/consume input stream.
Please note that if redirected/piped this way new command window will not stay open It will maybe :-) just execute your command and then close/exit.
Create a CMD script to run both of the commands that you have shown in the question. Maybe call it RunMyProgram.cmd. The contents are just the two lines that you have:
REM Source the environment variables.
REM Any new command prompt window that is opened can be ignored
CALL C:\Batch\MyBatFile.bat
C:\Program\MyProgram.exe
If what you have stated in the comments to your question is accurate regarding MyBatFile.bat setting up the environment variables and then starting a new window, then you should be able to make use of those environment variables after MyBatFile.bat exits.
If running RunMyProgram.cmd from a command prompt still has MyProgram.exe giving the error when the environment variable is not set, or if MyProgram.exe doesn't even start to run until you close the new window that popped up, then we need to see the exact commands that MyBatFile.bat is executing.

How to create and execute a file full of commands on Windows command prompt?

Example:
In Linux we can put the desired commands in a file and give it executable permissions. This helps us to actually run the file on the terminal and thus all the commands inside the file get automatically executed.
How to achieve this on Windows XP?
Same thing, but it's called a batch file, extension is .bat. You can also double-click to run these. This site is a great resource.

How to create an executable command prompt script

I usually perform actions in the 7zip command line program. I was thinking about creating a small script to do everything automatically, but I have never written any Windows shell script before and therefore don't even know where to begin.
I would like to make an executable script file which, when a user double-clicks on it, will call the 7zip command line and perform some actions.
First of all, is this possible? And if it is, what is the best way to do this?
You can create a batch script to do this.
It's basically command line commands that run one after another so you don't have to keep typing them in :)
Put the commands you would normally use for 7zip in a notepad file and save it with the extension .bat, then run it.
7z blah blah params
7z more params and args
All your commands will be executed automatically when the previous one finishes.
There are other programming languages you could do this in (or even VBScript) but batch would be perfectly suited to this, and you don't need to install anything extra.
Batch files can run a series of command line commands. Simply create a text file and name it with the .bat extension.
There are plenty of resources on the internet which will provide you with help.

how to make my console in python not to close?

I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.
I don't know why this is happening. I need your help.
Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...
raw_input()
... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.
Run your program from a Windows command prompt. That will not automatically close when the program finishes.
If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).
Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:
python main.exe
pause
Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.

Why is it that Cygwin can run .bat scripts?

When I execute a .bat script from bash in Cygwin, by what mechanism is it running? I understand that if I run a .EXE it will launch, regardless of whether the .EXE is from Cygwin or from a more traditional environment. I understand that when I execute an executable script with #! at the beginning that Cygwin supplies the magic for it to run.
But why does a .bat script work? Is there some component inside of Cygwin that is aware of what a Windows .bat script is and what to do with it? Or is it that it is somehow impossible under Windows to execute a call to launch a .EXE file that won't automatically also work for a .bat script instead?
Running
./test.bat params
from bash seems to be equivalent to
cmd /c test.bat params
I believe that bash in cygwin sees the bat extension as being flagged executable (a cygwin hit-tip to windows convention). As such it loads and executes the file with it's associated interpreter (cmd.exe, per os configuration), much as it creates a new instance of bash to run your #! scripts (per posix standard).
And if you want to fork an *.cmd file execution like a ShellScript process and append his log to an file:
cmd /c test.bat > nohup.out &
Enjoy!

Resources