Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run batch script without using *.bat extension

Is there any method in Windows through which we can execute a batch script without *.bat extension?

like image 991
Sree Avatar asked Nov 10 '12 08:11

Sree


People also ask

How do I run a script in batch mode?

In the local command prompt window, from the \bin directory, run the shell script, specifying the (pure. bat on Windows and pure on Linux) script name, along with the following arguments: -h <host_or_IP>


2 Answers

This is an interesting topic to me! I want to do some observations about it.

The important point first: A Batch file is a file with .BAT or .CMD extension. Period. Batch files can achieve, besides the execution of usual DOS commands, certain specific Batch-file facilities, in particular:

  • Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
  • Execution of GOTO command.
  • Execution of CALL :NAME command (internal subroutine).
  • Execution of SETLOCAL/ENDLOCAL commands.

Now the funny part: Any file can be redirected as input for CMD.exe so the DOS commands contained in it are executed in a similar way of a Batch file, with some differences. The most important one is that previous Batch-file facilities will NOT work. Another differences are illustrated in the NOT-Batch file below (I called it BATCH.TXT):

@echo off
rem Echo off just suppress echoing of the prompt and each loop of FOR command
rem but it does NOT suppress the listing of these commands!

rem Pause command does NOT pause, because it takes the character that follows it
pause
X

rem This behavior allows to put data for a SET /P command after it
set /P var=Enter data: 
This is the data for previous command!
echo Data read: "%var%"

rem Complex FOR/IF commands may be assembled and they execute in the usual way:
for /L %i in (1,1,5) do (
   set /P line=
   if "!line:~0,6!" equ "SHOW: " echo Line read: !line:~6!
)
NOSHOW: First line read
SHOW: Second line
NOSHOW: This is third line
SHOW: The line number 4
NOSHOW: Final line, number five

rem You may suppress the tracing of the execution redirecting CMD output to NUL
rem In this case, redirect output to STDERR to display messages in the screen
echo This is a message redirected to STDERR >&2

rem GOTO command doesn't work:
goto label
goto :EOF
rem but both EXIT and EXIT /B commands works:
exit /B

:label
echo Never reach this point...

To execute previous file, type: CMD /V:ON < BATCH.TXT The /V switch is needed to enable delayed expansion.

More specialized differences are related to the fact that commands in the NOT-Batch file are executed in the command-line context, NOT the Batch-file context. Perhaps Dave or jeb could elaborate on this point.

EDIT: Additional observations (batch2.txt):

@echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.

rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2

echo Standard input/output operations> CON
echo/> CON
< CON set /P var=Enter value: > CON
echo/> CON
echo The value read is: "%var%"> CON

Execute previous file this way: CMD < BATCH2.TXT > NUL

EDIT: More additional observations (batch3.txt)

@echo off

rem Dynamic access to variables that usually requires DelayedExpansion via "call" trick

rem Read the next four lines; "next" means placed after the FOR command
rem (this may be used to simulate a Unix "here doc")

for /L %i in (1,1,4) do (
   set /P line[%i]=
)
Line one of immediate data
This is second line
The third one
And the fourth and last one...

(
echo Show the elements of the array read:
echo/
for /L %i in (1,1,4) do call echo Line %i- %line[%i]%
) > CON

Execute this file in the usual way: CMD < BATCH3.TXT > NUL

Interesting! Isn't it?

EDIT: Now, GOTO and CALL commands may be simulated in the NotBatch.txt file!!! See this post.

Antonio

like image 80
Aacini Avatar answered Oct 19 '22 02:10

Aacini


Just use:

type mybat.txt | cmd

Breaking it down...

type mybat.txt reads mybat.txt as a text file and prints the contents. The | says capture anything getting printed by the command on its left and pass it as an input to the command on its right. Then cmd (as you can probably guess) interprets any input it receives as commands and executes them.

In case you were wondering... you can replace cmd with bash to run on Linux.

like image 7
pullmyteeth Avatar answered Oct 19 '22 02:10

pullmyteeth