Windows使用Linux shell命令
眾所周知,Linux下的與windows的cmd不是一個(gè)量級的東西。對于使用慣了Linux,MacOS的經(jīng)常切換到cmd會抓狂。但是偶爾項(xiàng)目下又必須實(shí)現(xiàn)一些bat腳本。特別是在持續(xù)集成CI/CD部署時(shí)。
那么用windows bat調(diào)用gitbash,再調(diào)用Unix-like的命令的將是個(gè)很好的方法,至少能解決大部分需求。

gitbash.png
安裝git-bash
安裝 gitforwindows,https://gitforwindows.org (安裝不表),雖然主要是實(shí)現(xiàn)git客戶端,但是附帶的bash很有用。
安裝bash組件,并添加到系統(tǒng)參數(shù)(PATH),安裝后,任意文件夾右鍵,會出現(xiàn)【Git Bash Here】打開后就默認(rèn)進(jìn)入類似Linux的終端。
bat 調(diào)用shell
使用bat調(diào)用sh腳本,
新建一個(gè) test.bat 文件,寫入以下內(nèi)容:
echo "windows .bat call .shell script""
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "pwd"
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "cp a.txt b.txt"
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "./helloworld.sh"
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx' -H 'Content-Type: application/json' -d '{\"msgtype\": \"text\", \"text\": {\"content\":\"VIOM服務(wù)器58后端部署成功-\"}}'"
需要指定git-bash 安裝目錄
需要注意,部分windows字符,如雙引號,單引號需要使用\ 進(jìn)行轉(zhuǎn)義。
這樣就能實(shí)現(xiàn)
調(diào)用原生命令(在window下的bash只有部分Linux的系統(tǒng)命令)
調(diào)用常見的文件操作
調(diào)用本地shell腳本
調(diào)用curl外部命令等