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

5

9

I have a shell script called setup_wsl.sh which contains:

#!/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

When i run the script as ./setup_wsl.sh in WSL ( installed distro is ubuntu 18.04), errors occur as:

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:

The first command of the script works fine as it gives output "hai".

Can someone help me to find why these errors occured?

Mckim answered 20/3, 2019 at 10:22 Comment(0)
A
32

This is because of the carriage return in windows. Switch to the LF carriage with a text editor and save the new one to run the script or sed -i -e 's/\r$//' setup_wsl.sh

Aeriel answered 31/5, 2019 at 5:41 Comment(2)
Thanks a lot. I encountered the problem while using Linux VM on Windows via Vagrant and modifying synced sh files from Windows Notepad++ as CRLF. Just changed it to LF and it has worked.Coypu
This was a huge help for me. Fixed the problem.Aculeus
L
8

For any one who is using Notepad++ go to Edit => EOL Conversion => Unix(LF) same as show in the picture below enter image description here

Ladyship answered 29/7, 2020 at 4:55 Comment(0)
C
4

For VSCode, you just need to set it in the status bar

vscode return

More details and options here https://qvault.io/clean-code/line-breaks-vs-code-lf-vs-crlf/

Concoction answered 13/12, 2021 at 16:55 Comment(0)
C
0

As mentioned, this is caused by difference in CR-LF in Windows vs. *nix. However, since I didn't want to modify Notepad++ or my other editors (I use my files in multiple environments), I have a different solution. Simply use Dos2Unix once the files are transferred to a 8nix machine.

sudo apt-get install dos2unix
dos2unix *.sh
Coronal answered 25/11, 2022 at 6:46 Comment(0)
S
0

Running

> sudo -i

and then

> sudo apt-get update

does the trick

Seel answered 30/6, 2023 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.