raspberry pi, apt-get update in script does not work - bash

When I run:
sudo apt-get update
sudo apt-get upgrade
from the command line, it works.
If I put the same line a script file maintain.script:
echo UPDATING SYSTEM SOFTWARE – UPDATE
sudo apt-get update
echo UPDATING SYSTEM SOFTWARE – UPGRADE
sudo apt-get upgrade
and run:
sudo ./maintain.sh
I get errors:
E: Invalid operation update
E: Invalid operation upgrade
I have marked the script as an executable.
Updated After Comment from FSQ
Here is the script file:
#!/bin/bash
echo "UPDATING SYSTEM SOFTWARE – UPDATE"
apt-get update
echo "UPDATING SYSTEM SOFTWARE – UPGRADE"
apt-get upgrade
echo "UPDATING SYSTEM SOFTWARE – DISTRIBUTION"
apt-get dist-upgrade
echo "REMOVING APPLICATION ORPHANS"
apt-get autoremove –purge
echo "UPDATING FIRMWARE"
rpi-update
Here is the command:
pi#raspberrypi2 ~/projects $ sudo ./maintain.sh
Here is the result:
: not foundsh: 1: ./maintain.sh: #!/bin/bash
UPDATING SYSTEM SOFTWARE – UPDATE
E: Invalid operation update
UPDATING SYSTEM SOFTWARE – UPGRADE
E: Invalid operation upgrade
UPDATING SYSTEM SOFTWARE – DISTRIBUTION
E: Invalid operation dist-upgrade
REMOVING APPLICATION ORPHANS
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package –purge
UPDATING FIRMWARE
: not foundsh: 11: ./maintain.sh: rpi-update

It was a file format problem. I was editing the files over a folder share using Windows notepad, which uses a different \r\n to Linux.
Here is the command that corrected my script file:
sed -i 's/\r//' maintain.sh
Here is a script file I use to do all the script files in a folder, and make sure they are executable:
#!/bin/bash
echo "Correcting script file formats"
for file in *.sh
do
echo $file
sed -i 's/\r//' $file
chmod +x $file
done

Add this to the start of your script? #!/bin/bash
This is how it would work on ubuntu not sure about raspbian

Related

Trying to set GOPATH and GOROOT in AWS EC2 user data, but it is not working

I am trying to set up GOPATH GOROOT in my AWS EC2 Ubuntu 20.04 user data, but it never worked, every time I connect to the AWS EC2 and view the log in /var/log/cloud-init-output.log it always says
go: not found, but if I key in the echo part it will work.
I am trying to set up multiple EC2 with this basis, so I can't key in every instance myself.
The CloudFormation yaml user data part is below:
UserData:
Fn::Base64: |
#!/bin/bash
wget https://dl.google.com/go/go1.14.4.linux-amd64.tar.gz
tar -C /usr/local -zxvf go1.14.4.linux-amd64.tar.gz
mkdir -p ~/go/{bin,pkg,src}
echo 'export GOPATH=$HOME/go' >> ~/.bashrc
echo 'export GOROOT=/usr/local/go' >> ~/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin:$GOROOT/bin' >> ~/.bashrc
echo 'export GO111MODULE=auto' >> ~/.bashrc
source ~/.bashrc
apt -y update
apt -y install mongodb wget git
systemctl start mongodb
apt -y install git gcc cmake autoconf libtool pkg-config libmnl-dev libyaml-dev
go get -u github.com/sirupsen/logrus
cd ~
git clone --recursive https://github.com/williamlin0504/free5gcWithOCF.git
cd free5gcWithOCF
make
And here is the error inside /var/log/cloud-init-output.log
Error while user data runs
Is there anyone is familiar with this, please I need some help~
In your error message, in the Makefile at line 30 there is a program bin/amf being used
This program appears to be a shell script with a problem in line 1
The nature of the problem is "go: not found"
If you have the bare word "go" in line 1 of the shell script and the path cannot find it then this is what will happen
Probably you need to alter the last line of your userdata shell script to say
PATH=/usr/local/go/bin:$PATH make
I know you have a source command earlier in the script that is supposed to set this up but it doesn't do what you think it does

Just to Update and Upgrade in terminal

so what i want to do is to create a shell script to update and upgrade for that i just created an .sh file in which there are three lines of command
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
after running .sh file it executes just fine.
but what i want to do more with it is - after successfully running update command and while running upgrade command it ask us to get the archive with 'Y/N'.
can i do something in .sh so that i don't have to type 'y' or 'n' . i want y to be default.
On a debian based installation on Raspberry i using the short form of IF THEN ELSE like this...
apt update && apt -y full-upgrade || echo 'Hm, something failed!'
...and "The Matrix" asking nothing.

Refactoring bash script that uses several if statements [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a bash script that checks if an installation exists and installs it if it doesn't exist based on the path.
Is there a better way to refactor this script so that there isn't as many if statements? So far, I've looked at if [ -f /usr/bin/go -a -f /usr/bin/python3 but the issue is that the install portion of the script doesn't flow nicely.
# chmod 744 to provide execute permissions on this file
echo Installing VIM, Python3, Docker, and Git
# Install VIM
sudo apt install vim
# install the required plugins
# Install Python3 and IDLE
sudo apt update
sudo apt install python3 idle3
# Install Docker but check to see if Docker already exists
if test -f /usr/bin/docker; then
echo "docker already exists here so I am skipping this intall for you."
else
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
fi
# sudo usermod -aG docker <user-name> to run docker commands without using sudo
# Install Git but check to see if Git already exists
if test -f /usr/bin/git; then
echo "git already exists here so I am skipping this install for you."
else
sudo apt install git
fi
# Install Go but check to see if Go already exists
# After installation, review the version and read this blog post https://www.jeremymorgan.com/tutorials/raspberry-pi/install-go-raspberry-pi/
if test -f /usr/bin/go; then
echo "golange already exists here so I am skipping this install for you."
else
sudo apt install golang
fi
echo Everything should be installed but check the logs for errors, thank you for your patience.
I think of something like this, using a for loop:
exes=( docker git go )
for i in "${!exes[#]}"; do
if type -p "${exes[i]}" &>/dev/null; then
echo "${exes[i]} installed"
unset exes[i]
else
echo >&2 "${exes[i]} not installed"
fi
done
sudo apt install "${exes[#]}"

Getting the error in openvpn repo while running command apt-get update

I am getting the following error while running command "apt-get update":
E: Type 'gpg' is not known on line 1 in source list /etc/apt/sources.list.d/openvpn-as-repo.list
E: The list of sources could not be read.
Can someone please help me solve this error?
The below answer is taken from: https://askubuntu.com/questions/96967/how-do-i-fix-this-e-type-is-not-known-on-line-in-source-list-update
The error indicates a malformed entry in a source file, which causes the update process to abort. To fix it, you have to either fix the entry (if you know what the right entry should look like) or remove it altogether (that's what I'm going to describe, as it's the fastest way to enable you to update your system again).
First, you need to open the file containing the bad entry. The filename is given in the error message, in your example /etc/apt/sources.list.d/some-ppa.list. Open a terminal, and type
sudo nano /etc/apt/sources.list.d/some-ppa.list
and press Enter. After entering your password, you should now see the file opened in the text editor nano.
Now you need to locate the malformed entry. It should be on the line number given in the error message - in your case that would be line 1.
This line should be incomplete and start with the unknown type the update process is complaining about (here ain). Just delete the whole line, and save/close the file with Ctrl+X.
That's it. You should now be able to successfully run the update process.
It seems something went wrong. Redo these commands again.
# wget must be available. If wget is not installed and apt update is broken skip apt update in the next line.
sudo apt update && sudo apt -y install ca-certificates wget net-tools
wget -qO - https://as-repository.openvpn.net/as-repo-public.gpg | sudo apt-key add -
sudo sh -c 'echo "deb http://as-repository.openvpn.net/as/debian bionic main">/etc/apt/sources.list.d/openvpn-as-repo.list'
sudo apt update && sudo apt -y install openvpn-as

bash loop check command error then reiterate

Hi i'm writing a bash script that have to keep some installation process quick and easy for non dev-user.
I wrote this loop that has to check if the command sudo add-apt-repository give the error sudo: add-apt-repository: command not found and if so, need to run the command sudo apt-get install software-properties-common then reiterate the first command and go on with other instrucion
until sudo add-apt-repository; do
echo "sudo: add-apt-repository: command not found"
sudo apt-get install software-properties-common
done
...
other command
I tryed that loop but it don't run the second command under the echo. Can please someone help me fix that problem?
Thanks
Which behaviour do you have ? Because even if add-apt-repository is installed sudo add-apt-repository will fail
$ sudo add-apt-repository
Error: need a repository as argument
$ echo $?
1
Maybe, using "which" command is better, it returns 0 if the command exists and 1 if not.
Using conditional if instead of until should be enough to install the command.

Resources