This is my first bash script. I am trying to learn scripting and needed an idea. This is the first idea I came up with. When I used to distro hop I had to reinstall everything. This helped me out once I got the first part working. Then I wanted to add if then else for more than just Debian distros.
#!/bin/bash
#Script Name : app_install.sh
#Author inflatus
#Created 4 Sept 2015
#Version 0.4
#Description
#This script will determine the distributon of Linux and use the appropriate package manager to install my core apps
current_distro=`cat /etc/*-release | grep "^ID=" | grep -E -o "[a-z]\w+"`
close_distro=`cat /etc/*-release | grep "^ID_LIKE=" | grep -E -o "[a-z]\w+"`
echo "$current_distro"
echo "$close_distro"
if [ "$close_distro" = "debian" ]; then
#Debian
#Install my apps on a fresh install of a Debian based Linux
echo "This script will install your apps after a fresh install"
apt-get update && apt-get dist-upgrade -y
apt-get install htop traceroute whois gparted curl nmap openvpn rsync iptraf openssh-client git gimp hexchat unison -y
#Adding repositories
echo "Adding the repos"
add-apt-repository ppa:atareao/telegram -y
add-apt-repository ppa:mumble/release -y
add-apt-repository ppa:stebbins/handbrake-releases -y
add-apt-repository ppa:maarten-baert/simplescreenrecorder -y
add-apt-repository ppa:remmina-ppa-team/remmina-next -y
add-apt-repository ppa:libreoffice/libreoffice-5-1 -y
add-apt-repository ppa:wireshark-dev/stable -y
apt-get update
apt-get install telegram mumble handbrake simplescreenrecorder remmina remmina-plugin-rdp libfreerdp-plugins-standard libreoffice wireshark -y
apt-get dist-upgrade -y
#Time to clean
echo "Alright, cleaning now."
apt-get autoclean
#Finished install
echo "Finished."
elif [ "$close_distro" != "debian" ]; then
#Fedora
#Install my apps on a fresh install of a Fedora based Linux
echo "This script will install your apps after a fresh install"
dnf check-update -y
dnf update -y
dnf install htop traceroute jwhois gparted curl nmap openvpn rsync iptraf openssh-clients git gimp mumble remmina-plugins-rdp wireshark hexchat -y
dnf clean all
#Time to clean
echo "Alright, cleaning now."
dnf clean all
#Finished install
echo "Finished."
else
echo "This is not for you."
fi
1 Answer 1
Simplifying if-elif-else
The else
statement here is dead code, it can never happen:
if [ "$close_distro" = "debian" ]; then # commands to run if debian elif [ "$close_distro" != "debian" ]; then # commands to run if NOT debian else echo "This is not for you." fi
The conditions can be simplified to this:
if [ "$close_distro" = debian ]; then
# commands to run if debian
else
# commands to run if NOT debian
fi
I also dropped the unnecessary double-quoting around the literal string debian
.
Prefer $(...)
over `...`
When using sub-shells, instead the obsolete `...`
,
always use the modern syntax $(...)
.
Simplify a chain of commands with awk
when possible
In each of these commands, there are 3 processes executed: cat
, grep
and another grep
:
current_distro=`cat /etc/*-release | grep "^ID=" | grep -E -o "[a-z]\w+"` close_distro=`cat /etc/*-release | grep "^ID_LIKE=" | grep -E -o "[a-z]\w+"`
You can make that more efficient by rewriting each using a single awk
:
current_distro=$(awk -F= '1ドル == "ID" { print 2ドル }' /etc/*-release)
close_distro=$(awk -F= '1ドル == "ID_LIKE" { print 2ドル }' /etc/*-release)