I've dual linux boot i'm newbie in bash
when running the following script i got strange error:
if [[ 'grep -i fedora /etc/issue' ]]; then
echo "the OS is Fedora"
$(sudo yum update -y && sudo yum upgrade -y)
else
echo "the OS is Ubuntu"
$(sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y)
fi
error : ./server_update.sh: line 9: Loaded: command not found
It's attempting to execute the output of your apt-get/yum commands, lose the $(..)
You also have an issue at the start:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
is the correct way to check if a string exists.
Your code should then look like this:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
echo "the OS is Fedora"
sudo yum update -y && sudo yum upgrade -y
else
echo "the OS is Ubuntu"
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y
fi
Related
I'm writing a bash script here to install docker and send all outputs to the logs.txt file. But i still get errors such as the one below displayed on the terminal, what I'm i doing wrong here to get these errors?
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
if [[ `command -v apt-get` ]]; then
echo -e "\n${GREEN}[${WHITE}+${GREENS}]${GREENS} Getting requirements....."
sleep 1;
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release >> logs.txt
echo -e "\n${GREEN}[${WHITE}+${GREENS}]${GREENS} Adding Docker’s official GPG key........"
sleep 1;
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo -e "\n${GREEN}[${WHITE}+${GREENS}]${GREENS} Installing Docker......."
sleep 1;
sudo apt-get install -y docker-ce docker-ce-cli containerd.io >> logs.txt
echo -e "\n${GREEN}[${WHITE}+${GREENS}]${GREENS} Docker version........"
sleep 1;
docker --version | head -n1
This question already has answers here:
Meaning of "[: too many arguments" error from if [] (square brackets)
(6 answers)
Closed 2 years ago.
I am pretty new to Bash scripting and currently, working on creating an installer to install a Docker client with Docker-Compose, according to the user's OS. This script is not planned to work on each OS, the scope of this is only for Ubuntu 16.04, 18.04, CentOS 7 and 8.
I have written the below code to verify the user's OS and start the installation process (as for now, I'm trying to make it work for Ubuntu 18.04):
################
### Check OS ###
################
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
RESULT="$OS $VER"
printf -v $RESULT "Ubuntu 18.04"
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
# Older SuSE/etc.
...
elif [ -f /etc/redhat-release ]; then
# Older Red Hat, CentOS, etc.
...
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
echo $RESULT
#################################
### Ubuntu 18.04 Installation ###
#################################
if [ $RESULT = "Ubuntu 18.04" ]; then
echo "Installing on Ubuntu 18.04"
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce
sudo systemctl status docker
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
else
echo "not working"
fi
Running the above on Ubuntu 18.04 results with:
Ubuntu 18.04
./test.sh: line 46: [: too many arguments
not working
How can I compare the output of the $RESULT to a string? Any pointers or recommendations are more than welcome!
$RESULT contains whitespaces, quote it to avoid word
splitting:
if [ "$RESULT" = "Ubuntu 18.04" ]; then
I'm trying to learn how to write Bash scripts. I have this script to update my servers through ssh. I'm trying to add a check and a conditional to determine if the OS uses Yum or Apt then it will run the appropriate update commands. The if else statement seems to be wrong but I'm not sure how to correct this.
Here is the script:
#!/bin/bash
USERNAME="root"
HOSTS="host1 host2 host3"
apt_run="apt update && apt -y upgrade"
yum_run="yum check-update && yum -y update"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME}
find_os=$( command -v yum || command -v apt-get ) || echo "Neither
yum nor apt-get found"
if [[ $find_os='yum' ]]
then
"${yum_run}"
else
"${apt_run}"
fi
done
Here is my script for my virtual machines.
#!/bin/bash
hosts=(
leap151 kali ubuntu omv
)
for hostname in "${hosts[#]}"; do
ssh -t root#"$hostname" << 'EOF'
if type -P zypper >/dev/null; then
command zypper ref && command zypper up
elif type -P apt-get >/dev/null; then
command apt-get update && command apt-get upgrade
else
echo 'Neither zypper nor apt found!' >&2
exit 127
fi
EOF
done
Use an array for the host. Since you're using bash the builtin type is fine just for searching the executable within your PATH. See help type for more info. Use the -t option in ssh also use a heredoc just what I have/did. The exit 127 is what the shell would exit if there are no executable see man 1p exit.
I am trying to automate VM configuration with a script and am having some trouble getting access to some path variables that get set in either ~/.bashrc, ~/.bash_profile, or ~/.profile.
My remote VM is running ubuntu 14.04 LTS and I am deploying over ssh.
This is the array that gets joined together to be run as a bash command to configure the vm by installing nvm:
return [
rm -rf ~/.nvm,
sudo apt-get update,
sudo apt-get install -y build-essential libssl-dev,
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh,
bash install_nvm.sh,
echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile
].join('\n');
return [
`rm -rf ~/.nvm`,
`sudo apt-get update`,
`sudo apt-get install -y build-essential libssl-dev`,
`curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh`,
`bash install_nvm.sh`,
`echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile`
].join('\n');
But when when I run the next script that actually installs node and yarn, it cannot find nvm:
return [
`nvm install ${config.node.version}`,
`nvm use ${config.node.version}`,
`echo "using node $(node -v) and npm $(npm -v)"`,
`curl -o- -L https://yarnpkg.com/install.sh | bash`,
'echo "export PATH="$HOME/.yarn/bin:$PATH"" >> ~/.bash_profile',
].join('\n');
This is the error:
bash: nvm: command not found
bash: line 1: nvm: command not found`
I don't want to ssh in and manually add anything to any of the various profiles. I'd like it all to be done by the script. I also want to avoid sourcing ~/.nvm/nvm.sh or sourcing any of the profiles when the ssh session begins. I was under the impression that an ssh session automatically sources ~/.bash_profile, which should then read from those variables correct? If not, then how else can I configure my deployment script to automatically have access to these variables?
Based on the fact that you are using && as you said in your comments I would add a line to actually source ~/.nvm/nvm.sh before running the nvm commands. You likely don't have the command available at the shell until that has been run.
Change this:
return [
`rm -rf ~/.nvm`,
`sudo apt-get update`,
`sudo apt-get install -y build-essential libssl-dev`,
`curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh`,
`bash install_nvm.sh`,
`echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile`
].join('\n');
To this:
return [
`rm -rf ~/.nvm`,
`sudo apt-get update`,
`sudo apt-get install -y build-essential libssl-dev`,
`curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh`,
`bash install_nvm.sh`,
`echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile`,
`source ~/.nvm/nvm.sh`
].join('\n');
I just developed an opengl game in linux (ubuntu).. Now I would like to write a setup script for the same which installs the game directly into the apt using the command..
sudo apt-get install ...
so that it runs from anywhere throughout linux without going into the specified folder for the game. Anyone knows how to do that ?
http://blog.boxedice.com/2010/02/05/how-to-create-a-debian-deb-package/
http://ptspts.blogspot.com/2010/02/how-to-create-debianubuntu-package-deb.html
http://wiki.debian.org/HowToPackageForDebian
etc
#!/bin/bash
set -eu -o pipefail # fail on error , debug all lines
sudo -n true
test $? -eq 0 || exit 1 "you should have sudo priveledge to run this script"
echo installing the must-have pre-requisites
while read -r p ; do sudo apt-get install -y $p ; done < <(cat << "EOF"
perl
zip unzip
exuberant-ctags
mutt
libxml-atom-perl
postgresql-9.6
libdbd-pgsql
curl
wget
libwww-curl-perl
EOF
)
echo installing the nice-to-have pre-requisites
echo you have 5 seconds to proceed ...
echo or
echo hit Ctrl+C to quit
echo -e "\n"
sleep 6
sudo apt-get install -y tig