"E: Invalid operation update" 在 WSL 中执行 运行 shell 脚本时出错

"E: Invalid operation update" error while running shell scripts in WSL

我有一个名为 setup_wsl.sh 的 shell 脚本,其中包含:

#!/bin/bash

echo "hai"
sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"  
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

当我 运行 脚本作为 ./setup_wsl.sh 在 WSL 中(安装的发行版是 ubuntu 18.04),错误发生为:

hai
E: Invalid operation update
E: Unable to locate package
./setup_wsl.sh: 4: ./setup_wsl.sh: apt-transport-https: not found
./setup_wsl.sh: 5: ./setup_wsl.sh: ca-certificates: not found
curl: (3) Illegal characters found in URL
./setup_wsl.sh: 7: ./setup_wsl.sh: gnupg-agent: not found
: not found ./setup_wsl.sh: software-properties-common
: not found ./setup_wsl.sh:

脚本的第一个命令运行良好,因为它给出了输出 "hai"。

有人可以帮我找出为什么会出现这些错误吗?

这是因为windows中的回车return。 使用文本编辑器切换到 LF 回车并将新的回车保存到 运行 脚本 或者 sed -i -e 's/\r$//' setup_wsl.sh

对于任何使用 Notepad++ 的人,请转到编辑 => EOL 转换 => Unix(LF) 与下图相同

对于VSCode,您只需要在状态栏中设置即可

此处有更多详细信息和选项https://qvault.io/clean-code/line-breaks-vs-code-lf-vs-crlf/