Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 413
AnjanaAK Avatar asked Mar 20 '19 10:03

AnjanaAK


3 Answers

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

like image 148
onlyha Avatar answered Nov 06 '22 00:11

onlyha


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

like image 32
M.Ali El-Sayed Avatar answered Nov 06 '22 01:11

M.Ali El-Sayed


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/

like image 1
Rami Alloush Avatar answered Nov 06 '22 01:11

Rami Alloush