file - 在 WSL 中运行 shell 脚本时出现 "E: Invalid operation update"错误

标签 file sh sudo ubuntu-18.04 windows-subsystem-for-linux

我有一个名为 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

当我在 WSL 中将脚本作为 ./setup_wsl.sh 运行时(安装的发行版是 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 中的回车。
使用文本编辑器切换到 LF 小车并保存新小车以运行脚本
或 sed -i -e 's/\r$//' setup_wsl.sh

关于file - 在 WSL 中运行 shell 脚本时出现 "E: Invalid operation update"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55258430/

相关文章:

c - 从C中的字符数组中提取一组变量[linux]

ruby - 为什么 open ("url") 有时会返回 File 有时会返回 StringIO?

python - 读写特定内容

linux - 针对 grep 搜索运行变量字符串匹配?

unix - 你能告诉我以下代码的错误吗?

python - 在我的 virtualenv 中,我需要对所有命令使用 sudo

python - 文件上的第二个文件处理程序行为异常

linux - 从脚本向程序提供 STDIN

php - 使用 shell_exec ('passwd' ) 更改用户密码

sudo - 无法让 sudo 命令与 Ansible 一起使用