How does this Windows batch file run embedded javascript?
How does Windows know that this is JSCRIPT?
@set @junk=1 /*
@echo off
cscript //nologo //E:jscript %0 %*
goto :eof
*/
x = WScrip开发者_JS百科t.Arguments
Yr = x(0) ; Mo = x(1)
YS = "JanFebMarAprMayJunJulAugSepOctNovDec"
MN = Mo<1 || Mo>12 ? Mo : YS.substr(3*Mo-3, 3) // Month Name
WScript.echo(" ", Yr, " ", MN)
WScript.echo(" Mo Tu We Th Fr Sa Su")
WD = new Date(Yr, Mo-1, 1).getDay() ;
if (WD==0) WD = 7 // Week Day Number of 1st
LD = new Date(Yr, Mo, 0).getDate() // Last Day of month
Wk = "" ; for (D=1 ; D < WD ; D++) Wk += " "
for (D=1 ; D<=LD ; D++) {
Wk = Wk + " " + (D<10 ? "0"+D : D) ; WD++
if ((WD==8) || (D==LD)) { WScript.echo(Wk) ; WD = WD-7 ; Wk = "" }
}
WScript.echo(" ------ ")
Sample usage:
C:\batch>calendar.cmd 2014 7
2014 Jul
Mo Tu We Th Fr Sa Su
01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
------
It is quite simple really. The first line is valid batch file language for setting a shell variable and everything after the last space is ignored. It is also valid JSCRIPT for setting compile time variables, and the last two chars begin a Javascript comment which causes the rest of the batch file language lines to be ignored.
The cscript
line causes the same file %0
to be executed by JSCRIPT with the same arguments %*
. Then the batch goto statement uses :eof
which is a builtin label that represents the end of file.
If you are a beginner, and you spend your time learning how to apply JSCRIPT to the problems of Windows shell scripting, you can reapply your Javascript knowledge in the browser with web applications, with Windows HTML apps (.HTA), and even in shell scripting on Unix platforms that have Rhino or node.js installed.
精彩评论