vscode task 与 linux shell编程小记

最近工作中用到了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
原文地址:https://www.cnblogs.com/lokvahkoor/p/15363569.html