VSCode 开发 Shell 脚本

VSCode 开发 Shell 脚本

一、VSCode 插件

  • shellman:提供智能提示和自动补全功能。
  • shellcheck:提供语法检查的功能。
  • shell-format:提供代码格式化功能,快捷键:Alt+Shift+f
  • Code Runner:提供代码运行功能。

Linux 上脚本首行要声明 bash 解释器,Windows 上也一样。不过,路径要更改为安装 Git 的路径,例如: E:\SOFTWARE\Git\bin/bash

#!E:\SOFTWARE\Git\bin\bash

# basic for command
for test in Alabama Alaska Arizona
do
    echo The next state is $test
done

二、VSCode 实现 Shell 脚本调试

2.1、Windows 配置 SSH

2.1.1、通过「设置」安装

1、使用 Windows + I 快捷键打开「设置」,依次选择「系统」>「可选功能」。
2、点击「查看功能」,在搜索框中输入 OpenSSH 进行筛选。
3、根据需要勾选:
OpenSSH 客户端:可以使用 ssh 命令连接到其他支持 SSH 的设备。
OpenSSH 服务器:允许其他设备通过 SSH 连接到你的电脑。
4、选择好后点击「下一步」,然后点击「安装」开始安装过程。

2.1.2、使用 PowerShell 安装

1、使用 Windows + R 快捷键打开「运行」对话框,输入 powershell,然后按 Ctrl + Shift + Enter 以管理员权限打开 PowerShell 窗口。
2、执行以下命令查看 OpenSSH 安装状态:
3、根据需要安装 OpenSSH 客户端和服务器组件:

# 安装 OpenSSH 客户端
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# 安装 OpenSSH 服务器
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

2.1.3、在 Windows 中配置 OpenSSH 服务器

1、当你在 Windows 上安装了 OpenSSH 服务器之后,需要进行一些配置才能接受连接:
2、使用 Windows + R 快捷键打开「运行」对话框,输入 powershell,然后按 Ctrl + Shift + Enter 以管理员权限打开 PowerShell。

Set-Service -Name sshd -StartupType 'Automatic'

3、设置 SSHD 服务自动启动:

Start-Service sshd

4、检查 SSH 服务器是否在侦听 22 端口:

netstat -an | findstr /i ":22"

5、确保 Windows Defender 防火墙允许 TCP 22 端口的入站连接:

Get-NetFirewallRule -Name *OpenSSH-Server* | select Name, DisplayName, Description, Enabled

如规则丢失或被禁用,可以创建新规则:

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

6、可选)如果要修改配置,如设置用户访问权限,可以编辑 sshd_config 配置文件:

Start-Process Notepad C:\Programdata\ssh\sshd_config

7、根据需要修改配置文件,保存后关闭记事本。
8、完成更改后,重启 SSHD 服务应用配置:

Restart-Service sshd

按照这些步骤,你的 Windows 就能通过 SSH 协议接受连接了。

2.2、使用 VSCode 远程连接 linux 服务

安装 VSCode 插件:Remote - SSH & Bash Debug

输入:ssh root@192.168.10.102 进行连接。
Remote - SSH 连接后生成 .config

Host 192.168.10.102
  HostName 192.168.10.102
  User root

Bash Debug 配置 launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "bashdb",
            "request": "launch",
            "name": "Bash-Debug (type in script name)",
            "cwd": "${workspaceFolder}",
            "program": "${command:AskForScriptName}",
            "args": []
        },
        {
            "type": "bashdb",
            "request": "launch",
            "name": "Bash-Debug (select script from list of sh files)",
            "cwd": "${workspaceFolder}",
            "program": "${command:SelectScriptName}",
            "args": []
        },
        {
            "type": "bashdb",
            "request": "launch",
            "name": "Bash-Debug (hardcoded script name)",
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}/path/to/script.sh",
            "args": []
        },
        {
            "type": "bashdb",
            "request": "launch",
            "name": "Bash-Debug (simplest configuration)",
            "program": "${file}"
        }
    ]
}

参考:
【OpenSSH】Windows 上的 OpenSSH:安装、配置和使用指南
在 Windows 下用 VScode 构造 shell 脚本的 IDE
使用 vscode 实现 shell 脚本调试

评论

暂无

添加新评论