SlideShare una empresa de Scribd logo
1 de 24
我的 Windows 平台自動化經驗
基礎批次檔撰寫實務
Windows Batch Scripting
多奇數位創意有限公司
技術總監 黃保翕 ( Will 保哥 )
部落格:http://blog.miniasp.com/
介紹 Windows 批次檔
Windows Batch Scripting Introduction
認識 cmd.exe 命令直譯器 (1)
• 開啟命令提示字元 (Command Prompt)
– [開始] -> [程式集] -> [命令提示字元]
– Win-R  cmd
– Win-X-C 命令提示字元
– Win-X-A 命令提示字元 (系統管理員)
• 結束命令提示字元
– 輸入 EXIT 即可退出命令提示字元
• 查詢 cmd.exe 使用說明
– 輸入 CMD /? 可以查詢命令直譯器的基本用法
3
認識 cmd.exe 命令直譯器 (2)
• 查詢 cmd.exe 版本
– 輸入 VER 命令可以查詢目前執行的 Windows 版本
• 查詢 cmd.exe 命令清單
– 輸入 HELP 可以查詢命令提示字元下所有可用的命令
• 查詢命令清單使用說明
– 輸入 HELP 命令名稱 即可查詢特定命令的使用說明
• 兩種命令類型
– 內建命令 (Built-in commands)
– 外部命令 (External commands)
4
內建命令與外部命令
• 內建命令 (Built-in commands)
– 內建在 cmd.exe 直譯器的命令
– 無法變更、更名或用任意程式取代這些命令
– 完整的內建命令清單
• 外部命令 (External commands)
– 內建在 Windows 作業系統的獨立執行檔 ( *.exe )
– 執行時有機會可用其他同名的程式取代 Windows 內建的外部命令
– 大部分的外部命令都可以加上 /? 參數取得使用說明
– 完整的外部命令清單
• 更多的外部命令
– Native Win32 ports of some GNU utilities ( 透過 Chocolatey 安裝 )
關於 Windows 批次檔
• 主要用途
– 在 cmd.exe 命令直譯器的執行環境下執行一個或多個命令
• 可用副檔名
– *.bat
– *.cmd
• 簡單批次檔範例 ( sample.bat )
@ECHO OFF
ECHO Hello World!
PAUSE
6
了解 Windows 批次檔基礎觀念
Windows Batch Scripting Fundamental
命令直譯器如何運作
• 命令直譯器在解析命令時的邏輯相當精妙且複雜!
• 命令直譯器的運作方式
– 預設會在批次檔中逐行解譯命令
– 直接在命令提示字元執行命令與在批次檔中執行命令幾乎完全相同
– 在批次檔中執行每一行命令時,都會自動重新載入批次檔解析該行
– 每一行命令都會產生一個執行結果,並儲存在 ERRORLEVEL 變數中
• 解析命令時主要區分成以下四個部分:
– 變數代入 (Variable substitution)
– 特殊字元 (Quoting and escaping)
– 命令語法 (Syntax)
– 訊息轉向 (Redirection) 8
變數代入 (Variable substitution)
• 環境變數
– 內建環境變數 (Windows Environment Variables)
– 系統環境變數 (預設套用在所有使用者的執行程序)
– 使用者環境變數 (預設套用在目前使用者的執行程序)
• 自定義變數
– 可以透過 SET 內建命令來建立自定義變數
– SET foo=bar
• 命令直譯器內建變數
– 在命令直譯器環境下預設就會有的變數
• 取得變數有三種格式 (不區分英文大小寫)
– %varname%
– $0 .. $9 (傳入批次檔的第 n 個參數內容,其中 $0 為批次檔名 )
– $* (傳入批次檔的所有參數內容,以空白間格) 9
命令直譯器有哪些內建變數
變數名稱 變數值
%CD% 目前路徑 (Current Directory)
%TIME% 目前系統時間 ( HH:MM:SS.mm )
%DATE% 目前系統日期 ( 會依據系統地區設定格式來顯示 )
%RANDOM% 自動產生一個 0 到 32767 的亂碼 (數字)
%ERRORLEVEL% 取得最近一次執行命令的回傳代碼 ( 0 代表成功 )
%CMDCMDLINE% 取得目前命令直譯器的路徑
10
在批次檔中使用 % 的注意事項
• 在命令提示字元中
– 可直接執行 ECHO % 輸出一個百分比符號
• 但在執行批次檔時
– 要輸出百分比符號就必須寫成 ECHO %% 才行!
• 在批次檔中只要單獨出現一個 % 字元時,都會自動被忽略!
11
特殊字元 (Quoting and escaping)
• 正常來說,大部分的字元都不需要用雙引號括起來
• 需要加上雙引號的特殊字元有
– 空白字元
– &()[]{}^=;!'+,`~
– <>|
• 如果不用雙引號就必須使用 ^ 跳脫字元
• 使用範例
– ECHO "Hello <World>"
– ECHO Hello ^<World^>
– DIR "C:Documents and Settings" (因為有空白字元)
– ECHO Johnson ^& son
– ECHO A ^^ B 12
命令語法 (Syntax)
• 基本命令組成
– 命令名稱
– 命令參數
– 執行管線 (pipe) + 另一個命令
• 基本命令範例
– dir * | more
• 命令名稱:dir
• 命令參數:*
• 執行管線 + 另一個命令:| more
13
命令組合語法
• 要在「一行命令」中組合多個命令,可以用以下語法
• &
– 左邊的命令執行完畢 (無論成功或失敗),右邊的命令就會立刻執行
– 範例:dir *.txt & dir *.bat
• &&
– 左邊的命令執行成功 (ERRORLEVEL = 0),右邊的命令才會跑
– 範例:dir *.txt && dir *.bat
• ||
– 左邊的命令執行不成功 (ERRORLEVEL != 0),右邊的命令才會跑
– 範例:dir *.txt || dir *.bat
14
群組命令語法
• 要在「一行命令」中組合多個命令並集中輸入結果,可以用
小括號將命令群組起來,並統一輸出訊息
• ( )
– 範例:(dir *.txt & dir *.bat) > a.txt
– 範例:(dir *.txt && dir *.bat && echo Done.) > a.txt
– 範例:(dir *.txt & dir *.bat)
15
如何將一行命令拆解成多行命令
• 批次檔範例
IF 1 EQU 1 ECHO Equal & ECHO Indeed, equal
• 利用 ^ 將一行命令拆解成多行命令 (最後還是當成一行執行)
@ECHO OFF
IF 1 EQU 1 ^
ECHO Equal &^
ECHO Indeed, equal
• 利用 () 將一行命令拆解成多行命令 (可讀性較高) (更多範例)
@ECHO OFF
IF 1 EQU 1 (
ECHO Equal
ECHO Indeed, equal ) 16
訊息轉向 (Redirection)
• 標準訊息 IO 區分成三種
– 標準輸入 (standard input) 編號 0
– 標準輸出 (standard output) 編號 1
– 標準錯誤 (standard error) 編號 2
• 基本的訊息轉向語法
< filename
將特定檔案內容轉向到執行程序的標準輸入
> filename
將執行程序的標準輸出轉向到特定檔案 (覆寫檔案)
>> filename
將執行程序的標準輸出轉向到特定檔案 (附加內容到檔案)
17
進階的訊息轉向語法
• 2>NUL
– 將應用程式的標準錯誤轉向到 NUL (null device) (隱藏輸出的意思)
– dir *.txt 2>NUL
• 2> filename
– 將應用程式的標準錯誤轉向到檔案 (覆寫檔案)
• 2>> filename
– 將應用程式的標準錯誤轉向到檔案 (附加內容到檔案)
• 2>&1
– 將應用程式的標準錯誤轉向到標準輸入
– dir *.txt >listing.log 2>&1
• (echo Hello & echo World) >myfile.txt
– 將兩個命令的標準輸出結果合併後輸出到 myfile.txt 檔案中
18
Windows 批次檔小技巧
Windows Batch Scripting Tips & Tricks
實作 touch 命令
• C:WindowsSystem32touch.cmd
@ECHO OFF
IF EXIST %1 (
COPY /B %1+,, %1 >nul
) ELSE (
echo. 2>%1
)
20
設定應用程式別名
• C:WindowsSystem32np.cmd
@ECHO OFF
start notepad %1
21
深入了解批次檔的語法細節
• 認識 ERRORLEVEL 變數
• 進階字串處理
• 參數處理
• 萬用字元
• 取得使用者輸入
• 認識 %~ 特殊變數 (Percent tilde)
• 自訂函式
• 基本數值運算
• 尋找檔案
• 命令提示字元下的快速鍵
• 關於路徑
• 陣列處理
22
相關連結
• Windows Batch Scripting - Wikibooks, open books for an open world
• An A-Z Index of the Windows CMD command line - SS64.com
• Batch file - Wikipedia
• Batch Script Tutorial
• Environment variables / Command shell overview
23
聯絡資訊
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite
• Will 保哥的推特
– https://twitter.com/Will_Huang

Más contenido relacionado

La actualidad más candente

Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!Kent Chen
 
[嵌入式系統] 嵌入式系統進階
[嵌入式系統] 嵌入式系統進階[嵌入式系統] 嵌入式系統進階
[嵌入式系統] 嵌入式系統進階Simen Li
 
Abusing Symlinks on Windows
Abusing Symlinks on WindowsAbusing Symlinks on Windows
Abusing Symlinks on WindowsOWASP Delhi
 
PowerShell Functions
PowerShell FunctionsPowerShell Functions
PowerShell Functionsmikepfeiffer
 
Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014Noé Fernández-Pozo
 
Zone Based Policy Firewall
Zone Based Policy FirewallZone Based Policy Firewall
Zone Based Policy Firewallpitt2k
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Input output redirection linux
Input output redirection linuxInput output redirection linux
Input output redirection linuxTanishq Soni
 
Linux command ppt
Linux command pptLinux command ppt
Linux command pptkalyanineve
 
makefiles tutorial
makefiles tutorialmakefiles tutorial
makefiles tutorialvsubhashini
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDBLinaro
 
Handling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMHandling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMMin-Yih Hsu
 
Admin reseaux sous linux cours 3
Admin reseaux sous linux   cours 3Admin reseaux sous linux   cours 3
Admin reseaux sous linux cours 3Stephen Salama
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicJoseph Lu
 
Intro to Pentesting Jenkins
Intro to Pentesting JenkinsIntro to Pentesting Jenkins
Intro to Pentesting JenkinsBrian Hysell
 
syzkaller: the next gen kernel fuzzer
syzkaller: the next gen kernel fuzzersyzkaller: the next gen kernel fuzzer
syzkaller: the next gen kernel fuzzerDmitry Vyukov
 

La actualidad más candente (20)

Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!
 
LLVM introduction
LLVM introductionLLVM introduction
LLVM introduction
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to Makefile
 
[嵌入式系統] 嵌入式系統進階
[嵌入式系統] 嵌入式系統進階[嵌入式系統] 嵌入式系統進階
[嵌入式系統] 嵌入式系統進階
 
Abusing Symlinks on Windows
Abusing Symlinks on WindowsAbusing Symlinks on Windows
Abusing Symlinks on Windows
 
PowerShell Functions
PowerShell FunctionsPowerShell Functions
PowerShell Functions
 
Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014
 
Cours ACL IPv4 et IPv6
Cours ACL IPv4 et IPv6Cours ACL IPv4 et IPv6
Cours ACL IPv4 et IPv6
 
Zone Based Policy Firewall
Zone Based Policy FirewallZone Based Policy Firewall
Zone Based Policy Firewall
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Linux shell
Linux shellLinux shell
Linux shell
 
Input output redirection linux
Input output redirection linuxInput output redirection linux
Input output redirection linux
 
Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
 
makefiles tutorial
makefiles tutorialmakefiles tutorial
makefiles tutorial
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
Handling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMHandling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVM
 
Admin reseaux sous linux cours 3
Admin reseaux sous linux   cours 3Admin reseaux sous linux   cours 3
Admin reseaux sous linux cours 3
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
Intro to Pentesting Jenkins
Intro to Pentesting JenkinsIntro to Pentesting Jenkins
Intro to Pentesting Jenkins
 
syzkaller: the next gen kernel fuzzer
syzkaller: the next gen kernel fuzzersyzkaller: the next gen kernel fuzzer
syzkaller: the next gen kernel fuzzer
 

Similar a 我的 Windows 平台自動化經驗:基礎批次檔撰寫實務

Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command lineWen Liao
 
Cfengine培训文档 刘天斯
Cfengine培训文档 刘天斯Cfengine培训文档 刘天斯
Cfengine培训文档 刘天斯liuts
 
Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]yiditushe
 
工作站教學
工作站教學工作站教學
工作站教學nick hsu
 
使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式Will Huang
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharingYi-Jun Zheng
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash ShellLI Daobing
 
Php extension开发
Php extension开发Php extension开发
Php extension开发thinkinlamp
 
Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)fisher zheng
 
作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)Ying wei (Joe) Chou
 
Erlang高级原理和应用
Erlang高级原理和应用Erlang高级原理和应用
Erlang高级原理和应用Feng Yu
 
Aix操作系统培训文档
Aix操作系统培训文档Aix操作系统培训文档
Aix操作系统培训文档lwj2012
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8Shu-Yu Fu
 

Similar a 我的 Windows 平台自動化經驗:基礎批次檔撰寫實務 (20)

Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command line
 
Cfengine培训文档 刘天斯
Cfengine培训文档 刘天斯Cfengine培训文档 刘天斯
Cfengine培训文档 刘天斯
 
C+
C+C+
C+
 
C#
C#C#
C#
 
第1章 概论
第1章 概论第1章 概论
第1章 概论
 
建置Python開發環境
建置Python開發環境建置Python開發環境
建置Python開發環境
 
Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]
 
18 cpu02
18 cpu0218 cpu02
18 cpu02
 
工作站教學
工作站教學工作站教學
工作站教學
 
Gnu
GnuGnu
Gnu
 
gnutool
gnutoolgnutool
gnutool
 
使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash Shell
 
Php extension开发
Php extension开发Php extension开发
Php extension开发
 
Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)
 
作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)
 
Erlang高级原理和应用
Erlang高级原理和应用Erlang高级原理和应用
Erlang高级原理和应用
 
Aix操作系统培训文档
Aix操作系统培训文档Aix操作系统培训文档
Aix操作系统培训文档
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8
 

Más de Will Huang

深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)Will Huang
 
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境Will Huang
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索Will Huang
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!Will Huang
 
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)Will Huang
 
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)Will Huang
 
Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Will Huang
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點Will Huang
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門Will Huang
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)Will Huang
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)Will Huang
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Will Huang
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)Will Huang
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Will Huang
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Will Huang
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)Will Huang
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)Will Huang
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)Will Huang
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)Will Huang
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)Will Huang
 

Más de Will Huang (20)

深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
 
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
 
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
 
Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
 

我的 Windows 平台自動化經驗:基礎批次檔撰寫實務

  • 1. 我的 Windows 平台自動化經驗 基礎批次檔撰寫實務 Windows Batch Scripting 多奇數位創意有限公司 技術總監 黃保翕 ( Will 保哥 ) 部落格:http://blog.miniasp.com/
  • 2. 介紹 Windows 批次檔 Windows Batch Scripting Introduction
  • 3. 認識 cmd.exe 命令直譯器 (1) • 開啟命令提示字元 (Command Prompt) – [開始] -> [程式集] -> [命令提示字元] – Win-R  cmd – Win-X-C 命令提示字元 – Win-X-A 命令提示字元 (系統管理員) • 結束命令提示字元 – 輸入 EXIT 即可退出命令提示字元 • 查詢 cmd.exe 使用說明 – 輸入 CMD /? 可以查詢命令直譯器的基本用法 3
  • 4. 認識 cmd.exe 命令直譯器 (2) • 查詢 cmd.exe 版本 – 輸入 VER 命令可以查詢目前執行的 Windows 版本 • 查詢 cmd.exe 命令清單 – 輸入 HELP 可以查詢命令提示字元下所有可用的命令 • 查詢命令清單使用說明 – 輸入 HELP 命令名稱 即可查詢特定命令的使用說明 • 兩種命令類型 – 內建命令 (Built-in commands) – 外部命令 (External commands) 4
  • 5. 內建命令與外部命令 • 內建命令 (Built-in commands) – 內建在 cmd.exe 直譯器的命令 – 無法變更、更名或用任意程式取代這些命令 – 完整的內建命令清單 • 外部命令 (External commands) – 內建在 Windows 作業系統的獨立執行檔 ( *.exe ) – 執行時有機會可用其他同名的程式取代 Windows 內建的外部命令 – 大部分的外部命令都可以加上 /? 參數取得使用說明 – 完整的外部命令清單 • 更多的外部命令 – Native Win32 ports of some GNU utilities ( 透過 Chocolatey 安裝 )
  • 6. 關於 Windows 批次檔 • 主要用途 – 在 cmd.exe 命令直譯器的執行環境下執行一個或多個命令 • 可用副檔名 – *.bat – *.cmd • 簡單批次檔範例 ( sample.bat ) @ECHO OFF ECHO Hello World! PAUSE 6
  • 7. 了解 Windows 批次檔基礎觀念 Windows Batch Scripting Fundamental
  • 8. 命令直譯器如何運作 • 命令直譯器在解析命令時的邏輯相當精妙且複雜! • 命令直譯器的運作方式 – 預設會在批次檔中逐行解譯命令 – 直接在命令提示字元執行命令與在批次檔中執行命令幾乎完全相同 – 在批次檔中執行每一行命令時,都會自動重新載入批次檔解析該行 – 每一行命令都會產生一個執行結果,並儲存在 ERRORLEVEL 變數中 • 解析命令時主要區分成以下四個部分: – 變數代入 (Variable substitution) – 特殊字元 (Quoting and escaping) – 命令語法 (Syntax) – 訊息轉向 (Redirection) 8
  • 9. 變數代入 (Variable substitution) • 環境變數 – 內建環境變數 (Windows Environment Variables) – 系統環境變數 (預設套用在所有使用者的執行程序) – 使用者環境變數 (預設套用在目前使用者的執行程序) • 自定義變數 – 可以透過 SET 內建命令來建立自定義變數 – SET foo=bar • 命令直譯器內建變數 – 在命令直譯器環境下預設就會有的變數 • 取得變數有三種格式 (不區分英文大小寫) – %varname% – $0 .. $9 (傳入批次檔的第 n 個參數內容,其中 $0 為批次檔名 ) – $* (傳入批次檔的所有參數內容,以空白間格) 9
  • 10. 命令直譯器有哪些內建變數 變數名稱 變數值 %CD% 目前路徑 (Current Directory) %TIME% 目前系統時間 ( HH:MM:SS.mm ) %DATE% 目前系統日期 ( 會依據系統地區設定格式來顯示 ) %RANDOM% 自動產生一個 0 到 32767 的亂碼 (數字) %ERRORLEVEL% 取得最近一次執行命令的回傳代碼 ( 0 代表成功 ) %CMDCMDLINE% 取得目前命令直譯器的路徑 10
  • 11. 在批次檔中使用 % 的注意事項 • 在命令提示字元中 – 可直接執行 ECHO % 輸出一個百分比符號 • 但在執行批次檔時 – 要輸出百分比符號就必須寫成 ECHO %% 才行! • 在批次檔中只要單獨出現一個 % 字元時,都會自動被忽略! 11
  • 12. 特殊字元 (Quoting and escaping) • 正常來說,大部分的字元都不需要用雙引號括起來 • 需要加上雙引號的特殊字元有 – 空白字元 – &()[]{}^=;!'+,`~ – <>| • 如果不用雙引號就必須使用 ^ 跳脫字元 • 使用範例 – ECHO "Hello <World>" – ECHO Hello ^<World^> – DIR "C:Documents and Settings" (因為有空白字元) – ECHO Johnson ^& son – ECHO A ^^ B 12
  • 13. 命令語法 (Syntax) • 基本命令組成 – 命令名稱 – 命令參數 – 執行管線 (pipe) + 另一個命令 • 基本命令範例 – dir * | more • 命令名稱:dir • 命令參數:* • 執行管線 + 另一個命令:| more 13
  • 14. 命令組合語法 • 要在「一行命令」中組合多個命令,可以用以下語法 • & – 左邊的命令執行完畢 (無論成功或失敗),右邊的命令就會立刻執行 – 範例:dir *.txt & dir *.bat • && – 左邊的命令執行成功 (ERRORLEVEL = 0),右邊的命令才會跑 – 範例:dir *.txt && dir *.bat • || – 左邊的命令執行不成功 (ERRORLEVEL != 0),右邊的命令才會跑 – 範例:dir *.txt || dir *.bat 14
  • 15. 群組命令語法 • 要在「一行命令」中組合多個命令並集中輸入結果,可以用 小括號將命令群組起來,並統一輸出訊息 • ( ) – 範例:(dir *.txt & dir *.bat) > a.txt – 範例:(dir *.txt && dir *.bat && echo Done.) > a.txt – 範例:(dir *.txt & dir *.bat) 15
  • 16. 如何將一行命令拆解成多行命令 • 批次檔範例 IF 1 EQU 1 ECHO Equal & ECHO Indeed, equal • 利用 ^ 將一行命令拆解成多行命令 (最後還是當成一行執行) @ECHO OFF IF 1 EQU 1 ^ ECHO Equal &^ ECHO Indeed, equal • 利用 () 將一行命令拆解成多行命令 (可讀性較高) (更多範例) @ECHO OFF IF 1 EQU 1 ( ECHO Equal ECHO Indeed, equal ) 16
  • 17. 訊息轉向 (Redirection) • 標準訊息 IO 區分成三種 – 標準輸入 (standard input) 編號 0 – 標準輸出 (standard output) 編號 1 – 標準錯誤 (standard error) 編號 2 • 基本的訊息轉向語法 < filename 將特定檔案內容轉向到執行程序的標準輸入 > filename 將執行程序的標準輸出轉向到特定檔案 (覆寫檔案) >> filename 將執行程序的標準輸出轉向到特定檔案 (附加內容到檔案) 17
  • 18. 進階的訊息轉向語法 • 2>NUL – 將應用程式的標準錯誤轉向到 NUL (null device) (隱藏輸出的意思) – dir *.txt 2>NUL • 2> filename – 將應用程式的標準錯誤轉向到檔案 (覆寫檔案) • 2>> filename – 將應用程式的標準錯誤轉向到檔案 (附加內容到檔案) • 2>&1 – 將應用程式的標準錯誤轉向到標準輸入 – dir *.txt >listing.log 2>&1 • (echo Hello & echo World) >myfile.txt – 將兩個命令的標準輸出結果合併後輸出到 myfile.txt 檔案中 18
  • 19. Windows 批次檔小技巧 Windows Batch Scripting Tips & Tricks
  • 20. 實作 touch 命令 • C:WindowsSystem32touch.cmd @ECHO OFF IF EXIST %1 ( COPY /B %1+,, %1 >nul ) ELSE ( echo. 2>%1 ) 20
  • 22. 深入了解批次檔的語法細節 • 認識 ERRORLEVEL 變數 • 進階字串處理 • 參數處理 • 萬用字元 • 取得使用者輸入 • 認識 %~ 特殊變數 (Percent tilde) • 自訂函式 • 基本數值運算 • 尋找檔案 • 命令提示字元下的快速鍵 • 關於路徑 • 陣列處理 22
  • 23. 相關連結 • Windows Batch Scripting - Wikibooks, open books for an open world • An A-Z Index of the Windows CMD command line - SS64.com • Batch file - Wikipedia • Batch Script Tutorial • Environment variables / Command shell overview 23
  • 24. 聯絡資訊 • The Will Will Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的噗浪 – http://www.plurk.com/willh/invite • Will 保哥的推特 – https://twitter.com/Will_Huang

Notas del editor

  1. 我的 Windows 平台自動化經驗:基礎批次檔撰寫實務
  2. Parenthesis/Brackets - Windows CMD - SS64.com https://ss64.com/nt/syntax-brackets.html
  3. https://en.wikibooks.org/wiki/Windows_Batch_Scripting#Limitations