Simple .sh script - can I improve it?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
thatchunkylad198966
Level 2
Level 2
Posts: 56
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:
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
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 2
Level 2
Posts: 56
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 12
Level 12
Posts: 4248
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'm also Terminalforlife on GitHub.
thatchunkylad198966
Level 2
Level 2
Posts: 56
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 2
Level 2
Posts: 56
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 12
Level 12
Posts: 4248
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'm also Terminalforlife on GitHub.
thatchunkylad198966
Level 2
Level 2
Posts: 56
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

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'
thatchunkylad198966
Level 2
Level 2
Posts: 56
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 12
Level 12
Posts: 4248
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
UPDATE:

I've just noticed you have these lines:

Code: Select all

apt-get upgrade -y
apt-get full-upgrade -y
apt-get dist-upgrade -y
You have a lot of redundancies there, AFAIK. You should be able to just:

Code: Select all

apt-get -y dist-upgrade
Oddly, the full-upgrade parameter isn't documented for my version of apt-get(8), but it didn't throw up an error; I'm assuming it does the same as dist-upgrade.

Where you have these lines:

Code: Select all

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
I recommend you combine them to:

Code: Select all

systemctl enable lightdm NetworkManager vnstat smbd qbittorrent radarr nzbget sonarr jackett
Many commands with systemctl(8) can accept multiple things, like those for start, stop, enable, and restart.

And here's your `mkdir` section combined into a one-liner:

Code: Select all

mkdir -p {/etc/{lightdm,squid},/home/pi/{Pictures,Desktop,Downloads,.config/i3{,status},.fonts}}
But that won't exactly make things easier when it comes to making changes. :lol: A lot of your lines could take advantage of brace expansion, but it will add complexity if you're not familiar with it.

You have a lot of cd calls, which aren't really needed, as you could just use the full path instead of always changing to a directory then using a path relative to it. For example:

Code: Select all

$ cd Documents
$ mkdir NewDir
Instead of that, I could simply:

Code: Select all

$ mkdir Documents/NewDir
Personally, I've never been a fan of cd-ing everywhere in scripts, because it takes more brain(TM) to parse and just involves more typing for no gain.
I'm also Terminalforlife on GitHub.
thatchunkylad198966
Level 2
Level 2
Posts: 56
Joined: Wed May 05, 2021 9:11 am

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

Post by thatchunkylad198966 »

Termy wrote: Tue Jun 22, 2021 7:44 pm 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
UPDATE:

I've just noticed you have these lines:

Code: Select all

apt-get upgrade -y
apt-get full-upgrade -y
apt-get dist-upgrade -y
You have a lot of redundancies there, AFAIK. You should be able to just:

Code: Select all

apt-get -y dist-upgrade
Oddly, the full-upgrade parameter isn't documented for my version of apt-get(8), but it didn't throw up an error; I'm assuming it does the same as dist-upgrade.

Where you have these lines:

Code: Select all

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
I recommend you combine them to:

Code: Select all

systemctl enable lightdm NetworkManager vnstat smbd qbittorrent radarr nzbget sonarr jackett
Many commands with systemctl(8) can accept multiple things, like those for start, stop, enable, and restart.

And here's your `mkdir` section combined into a one-liner:

Code: Select all

mkdir -p {/etc/{lightdm,squid},/home/pi/{Pictures,Desktop,Downloads,.config/i3{,status},.fonts}}
But that won't exactly make things easier when it comes to making changes. :lol: A lot of your lines could take advantage of brace expansion, but it will add complexity if you're not familiar with it.

You have a lot of cd calls, which aren't really needed, as you could just use the full path instead of always changing to a directory then using a path relative to it. For example:

Code: Select all

$ cd Documents
$ mkdir NewDir
Instead of that, I could simply:

Code: Select all

$ mkdir Documents/NewDir
Personally, I've never been a fan of cd-ing everywhere in scripts, because it takes more brain(TM) to parse and just involves more typing for no gain.
Thanks so much!
THIS is why I joined this forum, the knowledge here is awesome.

Enjoy your day. :mrgreen:
llovepancakes
Level 1
Level 1
Posts: 34
Joined: Sun Jun 21, 2020 11:02 pm

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

Post by llovepancakes »

i like your idea, can you post a final outcome script for us to edit and try ourselves? if you have time :)

do you use docker with jackett? on your main pc without a virtualbox? docker free version? pro paid? and you probably use flaresolverr with it? ever use flaresolverr without jackett to browse websites that see cloudfire dns and stop websites loading? or has anyone done something similar?
thatchunkylad198966
Level 2
Level 2
Posts: 56
Joined: Wed May 05, 2021 9:11 am

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

Post by thatchunkylad198966 »

llovepancakes wrote: Sun Jul 04, 2021 11:55 am i like your idea, can you post a final outcome script for us to edit and try ourselves? if you have time :)

do you use docker with jackett? on your main pc without a virtualbox? docker free version? pro paid? and you probably use flaresolverr with it? ever use flaresolverr without jackett to browse websites that see cloudfire dns and stop websites loading? or has anyone done something similar?

Code: Select all

#!/bin/bash

### MOTD

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

### MKDIR

mkdir -p /etc/squid/
mkdir -p /home/pi/Pictures/
mkdir -p /home/pi/Desktop/
mkdir -p /home/pi/Downloads/

### SOFTWARE-PROPERTIES

apt-get install --no-install-recommends software-properties-common -y
apt-get update -y
add-apt-repository ppa:mozillateam/ppa -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 mesa-utils resolvconf ufw obconf android-tools-adb android-tools-fastboot hdparm wget curl git lbzip2 bzip2 p7zip-full unrar xarchiver engrampa udisks2 udiskie ntfs-3g gvfs gvfs-fuse gvfs-backends gparted samba smbclient cifs-utils neofetch sakura mousepad hexchat hexchat-plugins kodi uget gnome-disk-utility gnome-screenshot gnome-icon-theme lxappearance python2 python3 openjdk-11-jdk wireguard-tools openvpn network-manager network-manager-gnome network-manager-openvpn network-manager-openvpn-gnome gimp vnstat squid xserver-xorg-video-fbdev xserver-xorg xinit lxqt lxqt-config lxqt-core lxqt-themes vlc mpv firefox-esr xfce4-settings oxygen-icon-theme openbox thunar thunar-volman pcmanfm pcmanfm-qt screen libexo-1-0 htop powertop mediainfo docker.io -y

### GIT-CLONE

cd /home/pi/
git clone https://github.com/iSc0ttyathon/custom-rpi4-os.git
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/

### 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 rc-local
systemctl disable --now zramswap
systemctl disable --now nmbd
systemctl disable --now squid

### ENABLING-SERVICES

systemctl enable NetworkManager
systemctl enable vnstat
systemctl enable smbd

### UPDATING-THE-KERNEL

fetch -u -1

### DISABLING-THE-FIREWALL

ufw disable

### SETTING-PERMISSIONS

chmod -R 0777 /home/
usermod -aG docker pi

### CONFIGURING-THE-OS

echo "//192.168.0.225/root/    /mnt/external/   cifs    username=*username*,password=*password*,uid=pi,gid=pi,file_mode=0777,dir_mode=0777   0    0" >> /etc/fstab
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
echo "exec startlxqt" >> /home/pi/.xinitrc
dpkg-reconfigure locales tzdata keyboard-configuration resolvconf

### CHROMIUM-DEBIAN

tee -a /etc/apt/sources.list.d/debian.list <<EOF
deb http://deb.debian.org/debian/ stable main #Debian stable
deb http://deb.debian.org/debian/ stable-updates main #Debian updates
deb http://deb.debian.org/debian-security stable/updates main #Debian security updates
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

### VULKAN-INSTALL

echo 1/8 Update
sudo apt-get update

echo 2/8 Upgrade
sudo apt-get full-upgrade -y

echo 3/8 Install pre-requisites
sudo apt-get install --no-install-recommends -y python3-pip libxcb-randr0-dev libxrandr-dev \
        libxcb-xinerama0-dev libxinerama-dev libxcursor-dev \
        libxcb-cursor-dev libxkbcommon-dev xutils-dev \
        xutils-dev libpthread-stubs0-dev libpciaccess-dev \
        libffi-dev x11proto-xext-dev libxcb1-dev libxcb-*dev \
        bison flex libssl-dev libgnutls28-dev x11proto-dri2-dev \
        x11proto-dri3-dev libx11-dev libxcb-glx0-dev \
        libx11-xcb-dev libxext-dev libxdamage-dev libxfixes-dev \
        libva-dev x11proto-randr-dev x11proto-present-dev \
        libclc-dev libelf-dev git build-essential mesa-utils \
        libvulkan-dev ninja-build libvulkan1 python3-mako \
        libdrm-dev libxshmfence-dev libxxf86vm-dev libunwind-dev \
		valgrind libzstd-dev vulkan-tools

echo 4/8 Install meson
sudo apt purge meson -y
sudo pip3 install meson 

echo 5/8 Install mako
sudo pip3 install mako

echo 6/8 Get v3dv
cd ~
git clone https://gitlab.freedesktop.org/mesa/mesa.git mesa_vulkan

echo 7/8 Build v3dv
cd mesa_vulkan
CFLAGS="-mcpu=cortex-a72" CXXFLAGS="-mcpu=cortex-a72" meson --prefix /usr -Dplatforms=x11 -Dvulkan-drivers=broadcom -Ddri-drivers= -Dgallium-drivers=kmsro,v3d,vc4 -Dbuildtype=release build
ninja -C build -j4
sudo ninja -C build install

### 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

### CLEANING-UP-THE-OS

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

### DELETING-LEFT-OVER-FILES

rm -rf /home/pi/custom-rpi4-os
rm -rf /usr/local/bin/install-de
rm -rf /usr/local/bin/run-setup

### 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

### REBOOTING-THE-OS

shutdown -r now
That's the final one I've been using; it's based on a RasperryPi CM4 8GB. it installs a DE and Vulkan.
llovepancakes
Level 1
Level 1
Posts: 34
Joined: Sun Jun 21, 2020 11:02 pm

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

Post by llovepancakes »

thank u :D
Locked

Return to “Scripts & Bash”