Simple .sh script - can I improve it?

About writing shell scripts and making the most of your shell
Forum rules
Before you post please read how to get help
Post Reply
thatchunkylad198966
Level 1
Level 1
Posts: 29
Joined: Wed May 05, 2021 9:11 am

Simple .sh script - can I improve it?

Post by thatchunkylad198966 »

As I find myself changing OS quite often, I have a "simple" .sh script that git clones from github and moves stuff around.
I'm not the best with scripts; can I make it better?

Code: Select all

#!/bin/bash
sudo dpkg-reconfigure locales tzdata keyboard-configuration
sudo rm -f /etc/motd
sudo mv -f /etc/new.motd /etc/motd
sudo update-ca-certificates -f
apt-get update -y
apt-get upgrade -y
apt-get full-upgrade -y
apt-get dist-upgrade -y
apt-get autoremove --purge -y
apt-get autoclean -y
apt-get install --no-install-recommends software-properties-common -y
add-apt-repository ppa:mozillateam/ppa -y
add-apt-repository ppa:qbittorrent-team/qbittorrent-stable -y
apt-get update -y
apt-get install --no-install-recommends drm-info neofetch htop powertop oxygen-icon-theme openjdk-17-jdk resolvconf ufw bsdmainutils libexo-2-0 ubuntu-restricted-extras xserver-xorg-video-fbdev xserver-xorg xinit vnstat squid lxqt-core xterm openbox openvpn wireguard network-manager network-manager-openvpn network-manager-openvpn-gnome network-manager-gnome iptables dnsutils whiptail iptables-persistent qrencode libqrencode4 wget curl git bzip2 lbzip2 p7zip xarchiver engrampa p7zip-full ntfs-3g gvfs gvfs-fuse gvfs-backends udiskie udisks2 thunar thunar-volman pcmanfm pcmanfm-qt samba smbclient cifs-utils sakura mousepad gimp firefox firefox-esr gparted hdparm gnome-icon-theme gnome-disk-utility gnome-screenshot i3-wm i3status i3lock i3blocks dmenu feh lightdm lightdm-gtk-greeter lightdm-gtk-greeter-settings python2 python3 mpv vlc pavucontrol pulseaudio volumeicon-alsa hexchat hexchat-plugins uget qbittorrent-nox qbittorrent xfwm4 lxappearance -y 
rm -rf /etc/samba/smb.conf
{ echo [global] ; echo netbios name = PiDesktop ; echo workgroup = WORKGROUP ; echo interfaces = 192.168.0.1/8 ; echo ; echo [hdd] ; echo comment = linux ; echo path = /mnt/ ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; echo ; echo [root] ; echo comment = Linux ; echo path = / ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; echo ; echo [home] ; echo comment = Linux ; echo path = /home/ ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; } >> /etc/samba/smb.conf
mkdir -p /etc/lightdm/
mkdir -p /home/pi/.config/lxqt
mkdir -p /etc/squid/
mkdir -p /home/
mkdir -p /home/pi/
mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/
mkdir -p /home/pi/.config/i3/
mkdir -p /home/pi/.config/i3status/
mkdir -p /home/pi/.fonts/
cd /home/pi/
git clone https://github.com/iSc0ttyathon/custom-rpi4-os.git
cd /home/pi/custom-rpi4-os/
rm -rf /boot/config.txt
cd /home/pi/custom-rpi4-os/
mv config /boot/config.txt
cd /home/pi/custom-rpi4-os/
mv qbittorrent /etc/systemd/system/qbittorrent-nox.service
cd /home/pi/custom-rpi4-os/
mv radarr /etc/systemd/system/radarr.service
cd /home/pi/custom-rpi4-os/
mv nzbget /etc/systemd/system/nzbget.service
cd /home/pi/custom-rpi4-os
mv feather.ttf /home/pi/.fonts/
cd /home/pi/custom-rpi4-os/
mv i3-status-config /home/pi/.config/i3status/config
cd /home/pi/custom-rpi4-os/
mv i3-wm-config /home/pi/.config/i3/config
cd /home/pi/custom-rpi4-os/
mv Desktop.png /home/pi/Pictures/Desktop.png
cd /home/pi/custom-rpi4-os/
mv RPi.jpg /home/pi/Pictures/RPi.jpg
rm -rf /etc/squid/squid.conf
cd /home/pi/custom-rpi4-os/
mv squid.conf /etc/squid/
rm -rf /etc/lightdm/lightdm-gtk-greeter.conf
cd /home/pi/custom-rpi4-os/
mv lightdm-gtk-greeter.conf /etc/lightdm/
cd /home/pi/custom-rpi4-os/
mv comp-off /usr/local/bin/
cd /home/pi/custom-rpi4-os/
mv firefox-sync /usr/local/bin/
cd /home/pi/custom-rpi4-os/
mv firefox-update /usr/local/bin/
cd /home/pi/custom-rpi4-os/
mv rpi-remove /usr/local/bin/
cd /home/pi/custom-rpi4-os/
mv rpi-update /usr/local/bin/
cd /home/pi/custom-rpi4-os/
mv check-kms /usr/local/bin/
chmod +x /usr/local/bin/comp-off
chmod +x /usr/local/bin/firefox-sync
chmod +x /usr/local/bin/firefox-update
chmod +x /usr/local/bin/rpi-remove
chmod +x /usr/local/bin/rpi-update
chmod +x /usr/local/bin/check-kms
apt-get update -y
apt-get upgrade -y
apt-get full-upgrade -y
apt-get dist-upgrade -y
apt-get autoremove --purge -y
apt-get autoclean -y
systemctl disable --now squid
systemctl disable --now nmbd
systemctl enable lightdm
systemctl enable NetworkManager
systemctl enable vnstat
systemctl enable smbd
systemctl enable qbittorrent-nox
systemctl enable radarr
systemctl enable nzbget
ufw disable 
rm -rf /etc/hosts
rm -rf /etc/hostname
cd /home/pi/custom-rpi4-os/
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 2009837CBFFD68F45BC180471F4F90DE2A9B4BF8
echo "deb https://apt.sonarr.tv/ubuntu focal main" | sudo tee /etc/apt/sources.list.d/sonarr.list
apt-get update -y
apt-get upgrade -y
apt-get install --no-install-recommends gnupg ca-certificates sonarr mediainfo -y
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
apt-get update -y
apt-get upgrade -y
cd /opt/
wget https://github.com/Jackett/Jackett/releases/download/v0.17.1032/Jackett.Binaries.LinuxARM64.tar.gz
tar -xzvf Jackett.Binaries.LinuxARM64.tar.gz
chown pi:pi -R /opt/Jackett
cd /opt/Jackett/
./install_service_systemd.sh
cd /home/pi/custom-rpi4-os
mv hostname /etc/hostname
cd /home/pi/custom-rpi4-os/
mv hosts /etc/hosts
cd /home/pi/custom-rpi4-os/
mv lxqt.conf /home/pi/.config/lxqt/
cd /home/pi/custom-rpi4-os/
mv lxqt-config-appearance.conf /home/pi/.config/lxqt/
systemctl enable radarr
systemctl enable nzbget
passwd pi
passwd root
smbpasswd -a pi
smbpasswd -a root
rm -rf /home/pi/custom-rpi4-os
cd /opt/
wget https://github.com/Radarr/Radarr/releases/download/v3.1.1.4954/Radarr.master.3.1.1.4954.linux-core-arm64.tar.gz
tar -xzvf Radarr.master.3.1.1.4954.linux-core-arm64.tar.gz
systemctl enable nzbget
systemctl enable radarr
add-apt-repository ppa:ubuntubudgie-dev/kodi -y
apt-get update -y
apt-get install --no-install-recommends kodi -y
chmod -R 0777 /home/
chmod -R 0777 /home/pi/
shutdown -r now
Have a great day. :mrgreen:
rene
Level 17
Level 17
Posts: 7543
Joined: Sun Mar 27, 2016 6:58 pm

Re: Simple .sh script - can I improve it?

Post by rene »

It's a bit much too comment fully on, but a few things I noticed, and only since you asked, ...
thatchunkylad198966 wrote:
Sat May 08, 2021 11:00 am

Code: Select all

sudo rm -f /etc/motd
sudo mv -f /etc/new.motd /etc/motd
There's no need for the rm given the following mv -f.
thatchunkylad198966 wrote:
Sat May 08, 2021 11:00 am

Code: Select all

{ echo [global] ; echo netbios name = PiDesktop ; echo workgroup = WORKGROUP ; echo interfaces = 192.168.0.1/8 ; echo ; echo [hdd] ; echo comment = linux ; echo path = /mnt/ ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; echo ; echo [root] ; echo comment = Linux ; echo path = / ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; echo ; echo [home] ; echo comment = Linux ; echo path = /home/ ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; } >> /etc/samba/smb.conf
Works, but I would personally use a so-called heredoc(ument) for this:

Code: Select all

cat << EOF  > /etc/samba/smb.conf
[global]
netbios name = PiDesktop
[ .... ]
directory mask = 7777
EOF
Or, in fact, cp -f /from/some/where/else/smb.conf /etc/samba/.
thatchunkylad198966 wrote:
Sat May 08, 2021 11:00 am

Code: Select all

mkdir -p /home/
mkdir -p /home/pi/
mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/
mkdir -p /home/pi/.config/i3/
mkdir -p /home/pi/.config/i3status/
mkdir -p /home/pi/.fonts/
-p says to create parents so certainly there's no need for the first two of those. I myself furthermore tend to use bash brace-expansion a fair bit:

Code: Select all

mkdir -p /home/pi/{Pictures,Desktop,Downloads,.config/i3{,status},.fonts}/
but I'd feel free to ignore that: generally, clearer = better. Otherwise I noticed that also e.g. chmod and systemctl can take multiple arguments at once, but yes, sure, see last sentence also.
thatchunkylad198966
Level 1
Level 1
Posts: 29
Joined: Wed May 05, 2021 9:11 am

Re: Simple .sh script - can I improve it?

Post by thatchunkylad198966 »

Thanks rene!
Even though I've been tinkering with Linux for quite some-time, I'm still learning!

Cheers.
User avatar
Termy
Level 7
Level 7
Posts: 1609
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Simple .sh script - can I improve it?

Post by Termy »

I agree with rene, but I'd also just add that I recommend you space out your code — think paragraphs. A wall of text in code is just as unpleasant as it is in a spoken language, IMO. See here, for an example of how I space out my code in a paragraph-like way.

Also, I use i3-wm, too. :D I take it you're also a fan?
I use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm Terminalforlife (TFL/LL) on YouTube: https://www.youtube.com/c/terminalforlife
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
thatchunkylad198966
Level 1
Level 1
Posts: 29
Joined: Wed May 05, 2021 9:11 am

Re: Simple .sh script - can I improve it?

Post by thatchunkylad198966 »

Termy wrote:
Mon Jun 07, 2021 10:17 pm
I agree with rene, but I'd also just add that I recommend you space out your code — think paragraphs. A wall of text in code is just as unpleasant as it is in a spoken language, IMO. See here, for an example of how I space out my code in a paragraph-like way.

Also, I use i3-wm, too. :D I take it you're also a fan?
Massive fan of i3-WM, much better than any 'normal' DE, IMHO.
I cleaned it up a bit;

Code: Select all

#!/bin/bash
apt-get update -y
apt-get install --no-install-recommends software-properties-common -y
apt-get update -y
mv -f /etc/new.motd /etc/motd
add-apt-repository ppa:mozillateam/ppa -y
add-apt-repository ppa:qbittorrent-team/qbittorrent-stable -y
add-apt-repository ppa:ubuntubudgie-dev/kodi -y
apt-get update -y
apt-get install --no-install-recommends snap snapd chromium-browser kodi iputils-ping neofetch lxappearance wget curl git lbzip2 bzip2 p7zip-full xarchiver engrampa samba smbclient cifs-utils ntfs-3g gvfs gvfs-fuse gvfs-backends udisks2 udiskie openjdk-17-jdk wireguard-tools openvpn hexchat hexchat-plugins kodi uget qbittorrent-nox qbittorrent network-manager network-manager-gnome network-manager-openvpn network-manager-openvpn-gnome i3-wm i3status i3lock i3blocks dmenu feh lightdm lightdm-gtk-greeter-settings lightdm-gtk-greeter xserver-xorg-video-fbdev xserver-xorg xinit gparted gnome-disk-utility gnome-screenshot gnome-icon-theme hdparm htop powertop python2 python3 gimp screen thunar thunar-volman pulseaudio pavucontrol mpv vlc xz-utils zram-tools pcmanfm pcmanfm-qt ufw volumeicon-alsa ubuntu-restricted-extras squid vnstat libexo-2-0 sakura mousepad firefox-esr resolvconf -y
apt-get autoremove --purge -y
apt-get autoclean -y
rm -rf /etc/samba/smb.conf
{ echo [global] ; echo netbios name = PiDesktop ; echo workgroup = WORKGROUP ; echo interfaces = 192.168.0.1/8 ; echo ; echo [hdd] ; echo comment = linux ; echo path = /mnt/ ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; echo ; echo [root] ; echo comment = Linux ; echo path = / ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; echo ; echo [home] ; echo comment = Linux ; echo path = /home/ ; echo browseable = yes ; echo guest ok = yes ; echo read only = no ; echo create mask = 7777 ; echo directory mask = 7777 ; } >> /etc/samba/smb.conf
mkdir -p /etc/lightdm/
mkdir -p /etc/squid/
mkdir -p /home/
mkdir -p /home/pi/
mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/
mkdir -p /home/pi/.config/i3/
mkdir -p /home/pi/.config/i3status/
mkdir -p /home/pi/.fonts/
cd /home/pi/
git clone https://github.com/iSc0ttyathon/custom-rpi4-os.git
cd /home/pi/custom-rpi4-os/
mv qbittorrent /etc/systemd/system/qbittorrent.service
cd /home/pi/custom-rpi4-os
mv feather.ttf /home/pi/.fonts/
cd /home/pi/custom-rpi4-os/
mv i3-status-config /home/pi/.config/i3status/config
cd /home/pi/custom-rpi4-os/
mv i3-wm-config /home/pi/.config/i3/config
cd /home/pi/custom-rpi4-os/
mv Desktop.png /home/pi/Pictures/Desktop.png
cd /home/pi/custom-rpi4-os/
mv RPi.jpg /home/pi/Pictures/RPi.jpg
rm -rf /etc/squid/squid.conf
cd /home/pi/custom-rpi4-os/
mv squid.conf /etc/squid/
rm -rf /etc/lightdm/lightdm-gtk-greeter.conf
cd /home/pi/custom-rpi4-os/
mv lightdm-gtk-greeter.conf /etc/lightdm/
cd /home/pi/custom-rpi4-os/
rm -rf /etc/hosts
rm -rf /etc/hostname
cd /home/pi/custom-rpi4-os
mv hostname /etc/hostname
cd /home/pi/custom-rpi4-os/
mv hosts /etc/hosts
systemctl disable --now squid nmbd zramswap rc-local
systemctl enable lightdm NetworkManager vnstat smbd qbittorrent
echo Changing password for user pi...
passwd pi
echo Changing password for user root...
passwd root
echo Changing samba password for user pi...
smbpasswd -a pi
echo Changing samba password for user root...
smbpasswd -a root
dpkg-reconfigure locales tzdata keyboard-configuration resolvconf
rm -rf /home/pi/custom-rpi4-os
rm -rf /usr/local/bin/run-setup
rm -rf /usr/local/bin/desktop-install-ubuntu
ufw disable 
chmod -R 0777 /home/
chmod -R 0777 /home/pi/
shutdown -r now
thatchunkylad198966
Level 1
Level 1
Posts: 29
Joined: Wed May 05, 2021 9:11 am

Re: Simple .sh script - can I improve it?

Post by thatchunkylad198966 »

Sorry for the double post:

Code: Select all

#!/bin/bash

### MOTD

mv -f /etc/new.motd /etc/motd

### MKDIR

mkdir -p /etc/lightdm/
mkdir -p /etc/squid/
mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/
mkdir -p /home/pi/.config/i3/
mkdir -p /home/pi/.config/i3status/
mkdir -p /home/pi/.fonts/

### DOWNLOAD

cd /opt/
wget https://github.com/Jackett/Jackett/releases/download/v0.18.354/Jackett.Binaries.LinuxARM64.tar.gz
tar -xzvf Jackett.Binaries.LinuxARM64.tar.gz
chown pi:pi -R /opt/Jackett
cd /opt/Jackett
./install_service_systemd.sh
cd /opt/
wget https://github.com/Radarr/Radarr/releases/download/v3.2.2.5080/Radarr.master.3.2.2.5080.linux-core-arm64.tar.gz
tar -xzvf Radarr.master.3.2.2.5080.linux-core-arm64.tar.gz

### SOFTWARE-PROPERTIES

apt-get install --no-install-recommends software-properties-common -y
apt-get update -y
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 2009837CBFFD68F45BC180471F4F90DE2A9B4BF8
echo "deb https://apt.sonarr.tv/ubuntu focal main" | sudo tee /etc/apt/sources.list.d/sonarr.list
add-apt-repository ppa:mozillateam/ppa -y
add-apt-repository ppa:qbittorrent-team/qbittorrent-stable -y
add-apt-repository ppa:ubuntubudgie-dev/kodi -y
add-apt-repository ppa:oibaf/graphics-drivers -y
apt-get update -y
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get full-upgrade -y

### STARTING-THE-INSTALL

apt-get install --no-install-recommends mono-devel libexo-2-0 ufw kodi lubuntu-desktop wget curl git lbzip2 bzip2 p7zip-full ntfs-3g xarchiver engrampa udisks2 udiskie gvfs gvfs-fuse gvfs-backends gparted sakura mousepad gnome-icon-theme gnome-disk-utility gnome-screenshot wireguard-tools openvpn network-manager network-manager-openvpn network-manager-openvpn-gnome gimp firefox firefox-esr hexchat hexchat-plugins openjdk-17-jdk python2 python3 uget qbittorrent vnstat squid neofetch thunar thunar-volman i3-wm i3status i3lock i3blocks dmenu feh unrar samba smbclient cifs-utils pcmanfm pcmanfm-qt lightdm lightdm-gtk-greeter lightdm-gtk-greeter-settings oxygen-icon-theme screen openssh-server resolvconf android-tools-adb android-tools-fastboot mpv vlc pulseaudio pavucontrol hdparm xz-utils qbittorrent-nox volumeicon-alsa update-manager -y

### GIT-CLONE

cd /home/pi/
git clone https://github.com/iSc0ttyathon/custom-rpi4-os.git
cd /home/pi/custom-rpi4-os/
mv qbittorrent /etc/systemd/system/qbittorrent.service
cd /home/pi/custom-rpi4-os
mv feather.ttf /home/pi/.fonts/
cd /home/pi/custom-rpi4-os/
mv i3-status-config /home/pi/.config/i3status/config
cd /home/pi/custom-rpi4-os/
mv i3-wm-config /home/pi/.config/i3/config
cd /home/pi/custom-rpi4-os/
mv Desktop.png /home/pi/Pictures/
cd /home/pi/custom-rpi4-os/
mv RPi.jpg /home/pi/Pictures/
rm -rf /etc/squid/squid.conf
cd /home/pi/custom-rpi4-os/
mv squid.conf /etc/squid/
rm -rf /etc/lightdm/lightdm-gtk-greeter.conf
cd /home/pi/custom-rpi4-os/
mv lightdm-gtk-greeter.conf /etc/lightdm/
cd /home/pi/custom-rpi4-os/
mv radarr /etc/systemd/system/radarr.service
cd /home/pi/custom-rpi4-os/
mv nzbget /etc/systemd/system/nzbget.service

### SETTING-HOSTS-AND-HOSTNAME

rm -rf /etc/hosts
rm -rf /etc/hostname
cd /home/pi/custom-rpi4-os
mv hostname /etc/
cd /home/pi/custom-rpi4-os/
mv hosts /etc/

### CONFIGURING-SAMBA

rm -rf /etc/samba/smb.conf
cd /home/pi/custom-rpi4-os/
mv smb.conf /etc/samba/

### DISABLING-SERVICES

systemctl disable --now squid nmbd zramswap rc-local sddm

### ENABLING-SERVICES

systemctl enable lightdm NetworkManager vnstat smbd qbittorrent

### CHANGING-OS-PASSWORDS

echo Changing password for the user pi...
passwd pi
echo Changing password for the user root...
passwd root
echo Changing samba password for the user pi...
smbpasswd -a pi
echo Changing samba password for the user root...
smbpasswd -a root

### MAKING-SURE-YOU-ARE-UP-TO-DATE

apt-get update -y
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get full-upgrade -y

### UPDATING-THE-KERNEL

fetch -u -1

### CLEANING-UP-THE-OS

apt-get autoremove --purge -y
apt-get autoclean -y

### DISABLING-THE-FIREWALL

ufw disable

### SETTING-PERMISSIONS

chmod -R 0777 /home/

### CONFIGURING-THE-OS

dpkg-reconfigure locales tzdata keyboard-configuration resolvconf

### DEBIAN-REPO-CHROMIUM

tee -a /etc/apt/sources.list.d/debian.list <<EOF
deb https://deb.debian.org/debian testing main contrib non-free
deb-src https://deb.debian.org/debian testing main contrib non-free
EOF
apt-get install --no-install-recommends debian-archive-keyring -y
apt-key add /usr/share/keyrings/debian-archive-keyring.gpg
apt-get update -y
apt-get install --no-install-recommends chromium -y
rm -rf /etc/apt/sources.list.d/debian.list
apt-get update -y

### DELETING-LEFT-OVER-FILES

cd /opt/
rm -rf Jackett.Binaries.LinuxARM64.tar.gz 
rm -rf Radarr.master.3.2.2.5080.linux-core-arm64.tar.gz
rm -rf /home/pi/custom-rpi4-os
rm -rf /usr/local/bin/install-de
rm -rf /usr/local/bin/run-setup

### REBOOTING-THE-OS

shutdown -r now
Much better than what it was.
User avatar
Termy
Level 7
Level 7
Posts: 1609
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Simple .sh script - can I improve it?

Post by Termy »

Yeah, that's more like it. :)
I use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm Terminalforlife (TFL/LL) on YouTube: https://www.youtube.com/c/terminalforlife
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
thatchunkylad198966
Level 1
Level 1
Posts: 29
Joined: Wed May 05, 2021 9:11 am

Re: Simple .sh script - can I improve it?

Post by thatchunkylad198966 »

I made a few error(s) and services wouldn't start. edited again, re-installed and now it's fine! :mrgreen:

Code: Select all

#!/bin/bash

### MOTD

mv -f /etc/new.motd /etc/motd

### MKDIR

mkdir -p /etc/lightdm/
mkdir -p /etc/squid/
mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/
mkdir -p /home/pi/.config/i3/
mkdir -p /home/pi/.config/i3status/
mkdir -p /home/pi/.fonts/

### DOWNLOAD

cd /opt/
wget https://github.com/Jackett/Jackett/releases/download/v0.18.354/Jackett.Binaries.LinuxARM64.tar.gz
tar -xzvf Jackett.Binaries.LinuxARM64.tar.gz
chown pi:pi -R /opt/Jackett
cd /opt/Jackett
./install_service_systemd.sh
cd /opt/
wget https://github.com/Radarr/Radarr/releases/download/v3.2.2.5080/Radarr.master.3.2.2.5080.linux-core-arm64.tar.gz
tar -xzvf Radarr.master.3.2.2.5080.linux-core-arm64.tar.gz

### SOFTWARE-PROPERTIES

apt-get install --no-install-recommends software-properties-common -y
apt-get update -y
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 2009837CBFFD68F45BC180471F4F90DE2A9B4BF8
echo "deb https://apt.sonarr.tv/ubuntu focal main" | sudo tee /etc/apt/sources.list.d/sonarr.list
add-apt-repository ppa:mozillateam/ppa -y
add-apt-repository ppa:qbittorrent-team/qbittorrent-stable -y
add-apt-repository ppa:ubuntubudgie-dev/kodi -y
add-apt-repository ppa:oibaf/graphics-drivers -y
apt-get update -y
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get full-upgrade -y

### STARTING-THE-INSTALL

apt-get install --no-install-recommends powertop htop mediainfo mesa-vdpau-drivers sonarr mono-devel libexo-2-0 ufw kodi lubuntu-desktop wget curl git lbzip2 bzip2 p7zip-full ntfs-3g xarchiver engrampa udisks2 udiskie gvfs gvfs-fuse gvfs-backends gparted sakura mousepad gnome-icon-theme gnome-disk-utility gnome-screenshot wireguard-tools openvpn network-manager network-manager-openvpn network-manager-openvpn-gnome gimp firefox firefox-esr hexchat hexchat-plugins openjdk-17-jdk python2 python3 uget qbittorrent vnstat squid neofetch thunar thunar-volman i3-wm i3status i3lock i3blocks dmenu feh unrar samba smbclient cifs-utils pcmanfm pcmanfm-qt lightdm lightdm-gtk-greeter lightdm-gtk-greeter-settings oxygen-icon-theme screen openssh-server resolvconf android-tools-adb android-tools-fastboot mpv vlc pulseaudio pavucontrol hdparm xz-utils qbittorrent-nox volumeicon-alsa update-manager lxappearance -y

### GIT-CLONE

cd /home/pi/
git clone https://github.com/iSc0ttyathon/custom-rpi4-os.git
cd /home/pi/custom-rpi4-os/
mv qbittorrent /etc/systemd/system/qbittorrent.service
cd /home/pi/custom-rpi4-os
mv feather.ttf /home/pi/.fonts/
cd /home/pi/custom-rpi4-os/
mv i3-status-config /home/pi/.config/i3status/config
cd /home/pi/custom-rpi4-os/
mv i3-wm-config /home/pi/.config/i3/config
cd /home/pi/custom-rpi4-os/
mv Desktop.png /home/pi/Pictures/
cd /home/pi/custom-rpi4-os/
mv RPi.jpg /home/pi/Pictures/
rm -rf /etc/squid/squid.conf
cd /home/pi/custom-rpi4-os/
mv squid.conf /etc/squid/
rm -rf /etc/lightdm/lightdm-gtk-greeter.conf
cd /home/pi/custom-rpi4-os/
mv lightdm-gtk-greeter.conf /etc/lightdm/
cd /home/pi/custom-rpi4-os/
mv radarr /etc/systemd/system/radarr.service
cd /home/pi/custom-rpi4-os/
mv nzbget /etc/systemd/system/nzbget.service

### SETTING-HOSTS-AND-HOSTNAME

rm -rf /etc/hosts
rm -rf /etc/hostname
cd /home/pi/custom-rpi4-os
mv hostname /etc/
cd /home/pi/custom-rpi4-os/
mv hosts /etc/

### CONFIGURING-SAMBA

rm -rf /etc/samba/smb.conf
cd /home/pi/custom-rpi4-os/
mv smb.conf /etc/samba/

### DISABLING-SERVICES

systemctl disable --now bluetooth
systemctl disable --now cups
systemctl disable --now sddm
systemctl disable --now rc-local
systemctl disable --now zramswap
systemctl disable --now nmbd
systemctl disable --now squid

### ENABLING-SERVICES

systemctl enable lightdm
systemctl enable NetworkManager
systemctl enable vnstat
systemctl enable smbd
systemctl enable qbittorrent
systemctl enable radarr
systemctl enable nzbget
systemctl enable sonarr
systemctl enable jackett

### CHANGING-OS-PASSWORDS

echo Changing password for the user pi...
passwd pi
echo Changing password for the user root...
passwd root
echo Changing samba password for the user pi...
smbpasswd -a pi
echo Changing samba password for the user root...
smbpasswd -a root

### MAKING-SURE-YOU-ARE-UP-TO-DATE

apt-get update -y
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get full-upgrade -y

### UPDATING-THE-KERNEL

fetch -u -1

### CLEANING-UP-THE-OS

apt-get autoremove --purge -y
apt-get autoclean -y

### DISABLING-THE-FIREWALL

ufw disable

### SETTING-PERMISSIONS

chmod -R 0777 /home/

### CONFIGURING-THE-OS

dpkg-reconfigure locales tzdata keyboard-configuration resolvconf

### DEBIAN-REPO-CHROMIUM

tee -a /etc/apt/sources.list.d/debian.list <<EOF
deb https://deb.debian.org/debian testing main contrib non-free
deb-src https://deb.debian.org/debian testing main contrib non-free
EOF
apt-get install --no-install-recommends debian-archive-keyring -y
apt-key add /usr/share/keyrings/debian-archive-keyring.gpg
apt-get update -y
apt-get install --no-install-recommends chromium -y
rm -rf /etc/apt/sources.list.d/debian.list
apt-get update -y

### DELETING-LEFT-OVER-FILES

cd /opt/
rm -rf Jackett.Binaries.LinuxARM64.tar.gz 
rm -rf Radarr.master.3.2.2.5080.linux-core-arm64.tar.gz
rm -rf /home/pi/custom-rpi4-os
rm -rf /usr/local/bin/install-de
rm -rf /usr/local/bin/run-setup

### REBOOTING-THE-OS

shutdown -r now
ajgringo619
Level 8
Level 8
Posts: 2378
Joined: Thu Mar 01, 2018 8:36 pm
Location: San Diego, California, USA

Re: Simple .sh script - can I improve it?

Post by ajgringo619 »

In your mkdir section, you can combine much of it into (1) command. For example, instead of:

Code: Select all

mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/
you could do this:

Code: Select all

$ mkdir -pv /home/pi/{Pictures,Desktop,Downloads}
mkdir: created directory '/home/pi'
mkdir: created directory '/home/pi/Pictures'
mkdir: created directory '/home/pi/Desktop'
mkdir: created directory '/home/pi/Downloads'
Mint 20.1 XFCE
Ryzen 7 2700X 3.7 GHz, GTX 1070/GTX 1050 Ti
BOINC Projects (Team Linux Mint): MilkyWay@Home | World Community Grid
Image
thatchunkylad198966
Level 1
Level 1
Posts: 29
Joined: Wed May 05, 2021 9:11 am

Re: Simple .sh script - can I improve it?

Post by thatchunkylad198966 »

ajgringo619 wrote:
Mon Jun 21, 2021 1:19 pm
In your mkdir section, you can combine much of it into (1) command. For example, instead of:

Code: Select all

mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/
you could do this:

Code: Select all

$ mkdir -pv /home/pi/{Pictures,Desktop,Downloads}
mkdir: created directory '/home/pi'
mkdir: created directory '/home/pi/Pictures'
mkdir: created directory '/home/pi/Desktop'
mkdir: created directory '/home/pi/Downloads'
Thank you! :mrgreen:
User avatar
Termy
Level 7
Level 7
Posts: 1609
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Simple .sh script - can I improve it?

Post by Termy »

To elaborate: the usage there with the braces is called brace expansion, because the values are expanded to what you want before the command is executed. Numeric and alphabetical ranges are supported, as is nesting.

Here are some examples with the full expanded command commented below each:

Code: Select all

cp FILE{,.bak}
# cp FILE FILE.bak

cp FILE{.bak,}
# cp FILE.bak FILE

echo {0..10}
# echo 0 1 2 3 4 5 6 7 8 9 10

echo {2..8..2}
# echo 2 4 6 8

echo {001..005}
# echo 001 002 003 004 005

find PATH/{OTHER_PATH,ANOTHER_PATH}
# find PATH/OTHER_PATH PATH/ANOTHER_PATH

touch FILE_{A..C}
# touch FILE_A FILE_B FILE_C

echo STRING{_{1..3},-{1.3}}
# echo STRING_1 STRING_2 STRING_3 STRING-1 STRING-2 STRING-3
I use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm Terminalforlife (TFL/LL) on YouTube: https://www.youtube.com/c/terminalforlife
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
Post Reply