1

I'm developing an angular-android application, so I have to run some commands in the cmd. My system has minimal specifications since the commands take some time to run, I have to keep on monitoring it. To reduce the time and concentrate on my other things I put some command in the batch file. For example

ng build
cordova clean browser
cordova build browser 
cordova run browser

I put the above commands and save it as a batch file. When I run this file it only executes first command ng build, it doesn't automatically jump to second line. Previously I used mac, in that I save these as .sh file and when I execute them, all the command inside that executed successfully. What should I do to execute next commands automatically in the batch file? Thank you.

e.k
  • 1,333
  • 1
  • 17
  • 36

2 Answers2

3

Some like this:


@echo off && setlocal enabledelayedexpansion

call ng &&^
call cordova clean browser &&^
call cordova build browser &&^
call cordova run browser 
endlocal && goto :EOF

rem :: or in one line ::

call ng && (call cordova clean browser && (call cordova build browser &&(call cordova run browser)))

  • Or ...
@echo off && setlocal enabledelayedexpansion

set "_run=ok"
call ng && for %%i in ("clean browser","build browser","run browser
)do if "!_run!" == "ok" call cordova %%~i || set "_run="
endlocal && goto :EOF

In / you can use the &, |, && and || operator, also combine them...


commandA &  commandB                    Run commandA and then run commandB
commandA && commandB                    Run commandA, if it succeeds then run commandB
commandA || commandB                    Run commandA, if it fails then run commandB
commandA && commandB || commandC        If commandA succeeds run commandB, if it fails commandC

Operator/Syntax Redirection in /

Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • Even if it works, it's dangerous, because if `ng` and `cordova` are batch files, then they implicitly exit the batch-file context to command-line context. That leads into strange side effects – jeb Feb 05 '20 at 12:35
  • @jeb, i've not know this is bat files.. know i understand the call suggetion – Io-oI Feb 05 '20 at 12:36
  • You'd need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) to make it work as intended or avoid it by using `if defined _run` – Stephan Feb 05 '20 at 14:33
  • @Stephan Danke schön! – Io-oI Feb 05 '20 at 14:50
0

add start before each of those commands

  start ng build 
  start cordova clean browser 
  start cordova build browser  
  start cordova run browser
Sara
  • 603
  • 8
  • 19