3
\$\begingroup\$

Extreme script newbie here, so please go easy on me!

I wrote a script to install zsh on a number of different Linux distributions. But since I'm so new at this, I'm piecing a lot of different things together and I doubt it's as efficient and tight as possible. Any suggestions are welcome. Thank you!

#!/bin/bash
export ZSHUSER=$USER
if [ -f /etc/os-release ]; then
 . /etc/os-release
 OS=$NAME
 VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
 OS=$(lsb_release -si)
 VER=$(lsb_release -sr)
fi
echo "Creating fonts directory"
cd /tmp && mkdir fonts && cd $_
echo "Downloading fonts"
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf
echo "Entering sudo mode to copy fonts"
if [[ $NAME = "Fedora" ]]; then
sudo su - <<EOF
dnf install -y git zsh zsh-autosuggestions zsh-syntax-highlighting
cd /usr/share/fonts/ && mkdir MesloMGS
cp /tmp/fonts/*.ttf /usr/share/fonts/MesloMGS
fc-cache
rm -r /tmp/fonts
scp -v /mnt/E/Linux/zsh/$NAME/aliasrc $HOME
chown $USER:$USER $HOME/aliasrc
su $USER
EOF
elif [[ $NAME = "void" ]]; then
sudo su - <<EOF
xbps-install -y git zsh zsh-autosuggestions zsh-syntax-highlighting
cp /tmp/fonts/*.ttf /usr/share/fonts/TTF
fc-cache
rm -r /tmp/fonts
scp -v /mnt/E/Linux/zsh/$NAME/aliasrc $HOME
chown $USER:$USER $HOME/aliasrc
su $USER
EOF
elif [[ $NAME = "Debian" ]] || [[ $NAME = "Ubuntu" ]] || [[ $NAME = "Linux Mint" ]]; then
sudo su - <<EOF
apt install -y git zsh zsh-autosuggestions zsh-syntax-highlighting
cd /usr/share/fonts/truetype && mkdir MesloMGS
cp /tmp/fonts/*.ttf /usr/share/fonts/truetype/MesloMGS
fc-cache
rm -r /tmp/fonts
scp -v /mnt/E/Linux/zsh/Debian/aliasrc $HOME
chown $USER:$USER $HOME/aliasrc
su $USER
EOF
elif [[ $NAME = "CentOS Linux" ]]; then
sudo su - <<EOF
yum install -y git zsh zsh-syntax-highlighting
cd /usr/share/fonts/ && mkdir MesloMGS
cp /tmp/fonts/*.ttf /usr/share/fonts/MesloMGS
fc-cache
rm -r /tmp/fonts
scp -v /mnt/E/Linux/zsh/CentOS/aliasrc $HOME
chown $USER:$USER $HOME/aliasrc
git clone https://github.com/zsh-users/zsh-autosuggestions /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
su $USER
EOF
else
echo "Operating system not identified"
pause
fi
mkdir ~/powerlevel10k && cd ~/powerlevel10k
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k
echo 'source ~/powerlevel10k/powerlevel10k.zsh-theme' >> ~/.zshrc
echo "opening terminal to configure zsh"
if [[ $NAME = "Ubuntu" ]] || [[ $NAME = "CentOS Linux" ]] || [[ $NAME = "Linux Mint" ]]; then
 gnome-terminal -q -- zsh
 read -p "press enter to resume"
elif [[ $NAME = "Fedora" ]]; then
 mate-terminal -e zsh
elif [[ $NAME = void ]]; then
 qterminal -e zsh
elif [[ $NAME = Debian ]]; then
 xfce4-terminal -e zsh
fi
sudo su - <<EOF
echo "copying .zshrc file"
scp -v /mnt/E/Linux/zsh/.zshrc $HOME
chown $USER:$USER $HOME/.zshrc
su $USER
EOF
```
asked Dec 20, 2020 at 0:59
\$\endgroup\$
2
  • \$\begingroup\$ In a professional environment, we much prefer stock bash rather than zsh, because we can just about guarantee that everyone can use it and that it is installed on the majority of systems by default. Zsh just brings unnecessary complications that we could really do without. e.g. the majority of shell programs run in bash and running them in zsh could break them in unexpected ways due to the differences in command output in zsh. I'm not arguing whether zsh or bash is better, just that bash is much more common and that zsh should be left for toys like macbooks rather than pro-tools like linux. \$\endgroup\$ Commented Dec 20, 2020 at 8:07
  • \$\begingroup\$ I appreciate that point of view. However, I'm just a college student looking for reasons to write scripts just to practice and get some practical application from it. Thanks. \$\endgroup\$ Commented Dec 20, 2020 at 19:47

1 Answer 1

2
\$\begingroup\$
if [[ $NAME = "Fedora" ]]; then
elif [[ $NAME = "void" ]]; then
elif [[ $NAME = "Debian" ]] || [[ $NAME = "Ubuntu" ]] || [[ $NAME = "Linux Mint" ]]; then

That looks like a candidate for case "$NAME" in. However, instead of changing that, let's look at what we're doing:

if [[ $NAME = "Fedora" ]]
then
 dnf install -y git zsh zsh-autosuggestions zsh-syntax-highlighting

Ah, so we're using that to determine which package manager is in use. It might be better to just test that directly:

if [ -x /usr/bin/dnf ]
then
 dnf install

That has the added benefit that it's plain POSIX shell, not requiring Bash.


Hang on, where did that $NAME come from? Let's look:

if [ -f /etc/os-release ]; then
 . /etc/os-release
 OS=$NAME
 VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
 OS=$(lsb_release -si)
 VER=$(lsb_release -sr)
fi

Oh dear, it looks like NAME is assigned in only one of those two paths, and we actually wanted to be using OS (or assigning to NAME and VERSION_ID in the elif branch, and getting rid of OS and VER).

This is why it's a good idea to set -u, so that you get an error when expanding an undefined variable.


echo "Operating system not identified"
pause

That should be echo >&2, since it's an error message. What's pause? It's certainly not a common tool, and you probably just want to exit 1 at that point anyway.


Not a big fan of copying those font files into /usr/share (that should be only for files under control of the package manager). That's what /usr/local is for.


Why are we using scp for a local copy? Just use plain cp and reduce the dependencies.


Many platforms have a configurable preferred terminal emulator, so use that first if it exists. For example /usr/bin/x-terminal-emulator on Debian.


On the use of sudo and su (not sure why you feel the need to double up like that!), I'd recommend turning this the other way around, and having a script that you must be root to run, but which drops privileges for the parts where that's possible. That's certainly better than being repeatedly asked for passwords (in many sudo configurations), which isn't something you want to be training users to do.

answered Feb 2, 2021 at 15:47
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.