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[#]}"
Related
I'm currently getting into linux and want to write a bash script which sets up a new machine just the way I want it to be.
In order to do that I want to install differnt things on it etc.
What I'm trying to achieve here is to have a setting at the top of the bash script which will make apt accept all [y/n] questions asked during the execution of the script
Question example I want to automatically accept:
After this operation, 1092 kB of additional disk space will be used. Do you want to continue? [Y/n]
I just started creating the file so here is what i have so far:
#!/bin/bash
# Constants
# Set apt to accept all [y/n] questions
>> some setting here <<
# Update and upgrade apt
apt update;
apt full-upgrade;
# Install terminator
apt install terminator
apt is meant to be used interactively. If you want to automate things, look at apt-get, and in particular its -y option:
-y, --yes, --assume-yes
Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively. If an undesirable
situation, such as changing a held package, trying to install an
unauthenticated package or removing an essential package occurs then
apt-get will abort. Configuration Item: APT::Get::Assume-Yes.
See also man apt-get for many more options.
With apt:
apt -o Apt::Get::Assume-Yes=true install <package>
See: man apt and man apt.conf
If you indeed want to set it up once at the top of the file as you say and then forget about it, you can use the APT_CONFIG environment variable. See apt.conf.
echo "APT::Get::Assume-Yes=yes" > /tmp/_tmp_apt.conf
export APT_CONFIG=/tmp/_tmp_apt.conf
apt-get update
apt-get install terminator
...
You can set up API assume yes permanently as follow:
echo "APT::Get::Assume-Yes \"true\";\nAPT::Get::allow \"true\";" | sudo tee -a /etc/apt/apt.conf.d/90_no_prompt
Another easy way to set it at the top of the your script is to use the command alias apt-get="apt-get --assume-yes", which causes all subsequent invocations of apt-get to include the --assume-yes argument. For example apt-get upgrade would automatically get converted to apt-get --assume-yes upgrade" by bash.
Please note, that this may cause errors, because some apt-get subcommands do not accept the --assume-yes argument. For example apt-get help would be converted to apt-get --assume-yes help which returns an error, because the help subcommand can't be used together with --assume-yes.
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.
This question already has answers here:
How can I check if a program exists from a Bash script?
(39 answers)
Closed 2 years ago.
I want to check whether a program like firefox exists on ubuntu or not. In case it is not installed, I want to install it. I studied this topic and got information about command -v p programName, but I didn't understand how can I check if the program is installed or not. I want to write this:
#If firefox not installed:
sudo apt-get update
sudo apt install firefox
but I don't know how to write the if condition part.
#!/usr/bin/env sh
if ! command -v firefox >/dev/null 2>&1
then
sudo apt-get update
sudo apt install firefox
fi
Also notice that not all Linux systems use apt-get and that if sudo is
configured to request a password the script will stall and wait for
user to type a password which might be confusing.
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
I am very new to bash scripting. I have the following script:
cp /etc/apt/sources.list /var/chroot/etc/apt/sources.list
chroot /var/chroot/
apt-get update
apt-get --simulate install $a > output
I actually want the last 2 comands to be run in chroot environment but I do not know how to give it to it, I searched but I could not find. I also want chroot to exit after execution of the commands, but it currently hangs. What can I do to prevent this?
EDIT: For future visitors:
cp /etc/apt/sources.list /var/chroot/etc/apt/sources.list
chroot /var/chroot apt-get update > /dev/null
chroot /var/chroot apt-get --simulate install nodejs
The command you want to run in the chroot environment must be given to chroot as an argument. See the manual page.