vscode task 與 linux shell程式設計小記
阿新 • • 發佈:2021-10-03
最近工作中用到了vscode task和shell指令碼,這邊做一些簡單的總結:
vscode task可以自動化地執行一些使用者預定義的命令動作,從而簡化日常開發中的一些繁瑣流程(比如編譯、部署、測試、程式組啟停等)
定義task的方式是編輯.vscode
目錄下的task.json
檔案,參考:Tasks in Visual Studio Code
一個典型的task配置:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "options": { // 這裡指定tasks 的執行目錄,預設是${workspaceRoot},也就是.vscode/.. "cwd": "${workspaceRoot}/build" }, "tasks": [ { // 這個task完成編譯工作,呼叫的是MSBuild "label": "build", "type": "shell", "command": "MSBuild.exe", "args": ["svr.sln","-property:Configuration=Release"] }, { // 這個task完成動態庫的拷貝 "label": "buildAll", "type": "shell", "command": "cp", "args": ["./src/odbc/Release/odbccpp.dll", "./Release/odbccpp.dll"], "dependsOn":["build"], // depend可以確保build完成之後再做拷貝 } , { // 使用好壓自動完成軟體的zip工作 "label": "product", "type": "shell", "command": "HaoZipC", "args": ["a", "-tzip", "./Product.zip", "./Release/*"], "dependsOn":["buildAll"], } ] }
同樣,我們也可以把複雜的命令組寫成bash指令碼,然後用一個task來呼叫這個指令碼。接下來分享一些基礎的bash指令碼知識點:
- bash檔案頭:
#!/bin/bash
- 使用變數:
something=3 # 變數賦值,注意等號兩邊不能有空格
echo $something # 列印變數
- 執行命令:
ping baidu.com # 直接打命令就ok
- 儲存命令執行結果為變數字串:
result1=$(ls)
result2=$(cd `dirname $0` && pwd) # 在命令引數中插入變數
- 在echo時列印字串變數:
echo "pwd: $(pwd)"
- 流程控制:
a=10
b=20
if [ $a == $b ]
then
echo "a 等於 b"
elif [ $a -gt $b ]
then
echo "a 大於 b"
elif [ $a -lt $b ]
then
echo "a 小於 b"
else
echo "沒有符合的條件"
fi
- 判斷字串間的包含關係,判斷字串是否為空:
# 判斷字串包含關係
strA="helloworld"
strB="low"
if [[ $strA =~ $strB ]]; then
echo "包含"
else
echo "不包含"
fi
# 判斷字串是否為空 STRING= if [ -z "$STRING" ]; then echo "STRING is empty" fi if [ -n "$STRING" ]; then echo "STRING is not empty" fi