issue commands to window/process started from batch file - batch-file

I want to pass commands to a process started in a batch file, specifically Cygwin. For instance, if I start Cygwin with something like the following:
start "window1" cmd.exe /c cygwin
How might I execute 'ls' in "window1", in the same batch file from which I started Cygwin?

I have to ask why you want to run the commands from a batch file? Do you want to take different actions based on the results/output of the commands?
In either case, what you are asking is bordering on impossible. Why don't you simply write your logic in a bash script and run
start "window1" cmd.exe /c c:\cygwin\bin\bash.exe -c [script]
where [script] is the path of your bash script.
Documentation on bash scripting is available at http://tldp.org/LDP/abs/html/

If you don't have cmd arg to cygwin to execute command in startup (like cmd.exe /K or /C) you can automate such thing with some script like AutoHotKey.

Related

how to run multiple command inside other terminal using batch file

i am using nvs, and there is a nvs command line using which i can change my node version.
currently i am using .bat file from Desktop and then go to nvs terminal
cd C:\nvs
start "C:\nvs\nvs.cmd"
this opens the terminal, and then i need to type more commands inside nvs terminal like
nvs use node/10.16.3/x64
cd ../code/api
node -v
code .
i need to automate this and all the executables to be part of the .bat file.
i tried using /c or /k to concatenate the commands. but the terminal closes before executing anything. how to run these commands as a part of batch file together.

ConEmu: how to open a standard windows cmd.exe window from conemu (and execute a command - e.g. start a batch file)

I need to open a legacy app which requires a standard cmd.exe
I would like to be able to spawn it from a batch file (.bat) and run it from command line in ConEmu
Don't see a way to do that.
start and cmd open a new tab in ConEmu at best - while I need the legacy cmd.exe window.
OK, it looks like I found a way - rather non-obvious but it seems to work:
[start] conhost cmd /c <your command>
or (if you want cmd window to continue beyond the execution of your command):
[start] conhost cmd /k <your command>
start in front is optional (useful if you don't want to block the original conemu terminal until completion of your command).

how to run pass docker terminal commands using .bat file

I Want to invoke docker compose using bat file.I have tried to invoke using the following but its not executing the commands.
this is my .bat file
echo on
cd C:\Program Files\Docker Toolbox\
start start.sh cd desktop
cd test
docker-compose up
Is there any other way to execute docker commands using bat file.or any other file.
A batch file is a script file. A script needs an interpreter. For a batch file the interpreter is cmd.exe – the Windows command interpreter.
A *.sh file is also a script which needs an interpreter. The interpreter is on Unix/Linux systems sh, bash, ksh, ... which are also executables but without file extension .exe because on Unix/Linux executables usually do not have a file extension.
On Windows there is no interpreter installed by default for Unix/Linux shell scripts. If start start.sh works at all than because of having installed a shell interpreter on Windows which has been registered in Windows registry as application for opening *.sh files which means interpreting the commands in the shell script file by started application.
But commands in a batch script interpreted by cmd.exe and executed within a Windows command environment can't be executed in shell environment of the shell interpreter.
start start.sh starts a new process running parallel to the Windows command process created for execution of the batch file. The batch file processing immediately continues after this line with executing the next command in command process while the shell interpreter process interprets parallel the commands in start.sh.
So what you need here is a batch file which creates a shell script to call start.sh and executes other shell commands in shell environment by shell interpreter.
The batch code below might work. It is not tested by me as I don't have Docker Toolbox nor any shell interpreter installed on my Windows machine.
#echo off
set "ShellScriptFile=%TEMP%\%~n0.sh"
( echo start.sh
echo cd desktop/test
echo docker-compose up
) >"%ShellScriptFile%"
start "Docker Compose" /D"%ProgramFiles%\Docker Toolbox" /wait "%ShellScriptFile%"
del "%ShellScriptFile%"
set "ShellScriptFile="
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~n0 (name of batch file without file extension and path)
del /?
echo /?
set /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of redirection operator > used here to create the shell script to execute with the 3 lines:
start.sh
cd desktop/test
docker-compose up
Those 3 lines are executed in the shell environment by shell script interpreter.
To run docker-compose via a bat file,
1. create a bat file eg run-docker-compose-up.bat
cd c:\docker
docker-compose up -d
pause
c:\docker is where I placed my docker-compose.yml
-d run containers in the background docker compose up docs
docker-compose must run as Administrator.
While a bat file itself cannot be marked as run as Administrator, you can either
right click on the bat file and choose 'Run as administrator'
or create a shortcut to the bat file and mark it as run as Administrator
(right click on shortcut, choose Properties, choose Advance, choose 'Run as administrator')
As I solved this problem, in a docker-compose case:
Go to your docker-compose.yml folder
On that, create a batchfile, such as: mydockerbatchfile.bat, using notepad (or other)
On this file, type your docker commands sequence', such as:
echo on
docker-compose down
docker container prune --force
docker system prune --volumes --force
docker image rm xxx/web-api
docker system df
docker image ls
pause
OBS: the commando pause will allow you check the results execution, in the screen)
Save -> make sure you saved in batch format/extension (mydockerbatchfile .bat)
In your windows desktop, create a ShortCut for the 'item 3 batch-file' (here - do not set the shortcut to "run as administrator")

Logging into Cygwin and executing commands using bat file in windows

I am using windows XP operating system and cygwin is installed in my C drive.
I need to login to cygwin directly to my directory path which contains a makefile and also a bash script called build.sh in the same directory. So i modified the original cygwin.bat file and added the line as shown below.
#echo off
C:
chdir C:\cygwin\bin
bash --login "/cygdrive/E/scheme_31july/build/build.sh"
When i double click on this bat file i could see my script executing but not on cygwin shell but on windows cmd shell as a result I get errors for "make" command like "No rule to make target" as make comes bundled with cygwin.
And when I explicitly login to cygwin using default cygwin.bat file and execute my script by giving following commands in cygwin shell the script executes without errors.
Basically I want to write a bat file so that I can keep it anywhere in my PC and instead of manually openeing the cygwin prompt and typing commands like:
$ cd /cygdrive/E/scheme_31july/build/
$ sh build.sh
it should happen automatically. I sit possible to do so.
Regards,
Harshit
No rule to make target sounds more like make being executed in the wrong directory. make itself seems to be available and running as intended.
Try this:
bash --login -c "cd /cygdrive/E/scheme_31july/build/ && sh build.sh"
This should start a --login session (which should give you access to all the settings and tools you'd expect in a cygwin prompt environment), then execute the given shell command, which is the cd and sh you asked for. You could also write those two lines to a separate script file, and pass the name of that to bash instead of the full path to build.sh.
You could also try to cd into C:\scheme_31july\build in the bat file and then execute bash from there. Not sure whether bash will try to change path upon entering the login session. You can try whether things work without the --login, both for this approach and the one above.
#echo off
C:
cd C:\scheme_31july\build
C:\cygwin\bin\bash.exe ./build.sh
I'm not sure whether you want the session to turn interactive after that or not. In the above case, bash will terminate after the script completed, and might even close the window. You might have to add a read into build.sh to avoid that. If you want bash to turn interactive after executing some command, you can try using the --rcfile option of bash to execute some commands and then turn interactive.

How to create a .BAT file opening program and preventing it from opening new windows?

So I am starting a consol program from .bat file. I want it to run as a process but not showing any windows. How to do such thing?
I think the start command with a '/B' option should do it ...
Windowless:
#echo off
start /B Myapp.exe
Minimized:
#echo off
start /MIN Myapp.exe
I'm not sure if I understand what you mean: if you run a console program from a .bat/.cmd script, that program will use the existing console window, not create a new one.
I suspect what you're really asking is how to prevent Explorer from opening a console window for the .bat/.cmd script itself. There's no built-in way to do that (although as amir75 suggested, you can mitigate it by running the script minimized; instead of running the script directly, create a shortcut to it and edit the shortcut's properties).
Alternatively, you can run your script through the silentbatch program that Paul Miner and I wrote.

Resources