[Example Script] Install Update Clean etc.

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

[Example Script] Install Update Clean etc.

Post by roncraig »

Hey guys, my first attempt at this... and it works great!

I know there will be many different ways to do the same thing but here is what is working for me at this time. Please feel free to modify and hack the heck out of this to suit your needs.

Create an empty text file named 'update'. Or simply issue the command 'touch update' to create the file then place the text below inside of it. Save the file then issue the command sudo chmod +x update to make it executable. Once that is done simply run the file by typing ./update

Code: Select all

#!/bin/bash
clear
echo "============================="
echo "=   U P D A T E   M E N U   ="
echo "============================="
echo "1. Full System Update/Upgrade"
echo "2. Clean APT Packages"
echo "3. Install balenaEtcher"
echo "4. Install Favorite Software"
echo "5. Exit/Quit"
echo "============================="
read -p "Enter Selection: " nmbr
if [ $nmbr -eq 1 ]
then
	sudo apt update
	sudo apt full-upgrade
	clear
	echo "System Updated - Returning to Menu"
	sleep 3s
	clear
	./update
	exit
elif [ $nmbr -eq 2 ]
then
	sudo apt autoclean
	sudo apt autoremove
	clear
	echo "System Cleaned - Returning to Menu"
	sleep 3s
	clear
	./update
	exit
elif [ $nmbr -eq 3 ]
then
	curl -1sLf \
	'https://dl.cloudsmith.io/public/balena/etcher/setup.deb.sh' \
	| sudo -E bash
	sudo apt-get update
	sudo apt-get install balena-etcher-electron
	clear
	echo "Etcher Installed - Returning to Menu"
	sleep 3s
	clear
	./update
	exit
elif [ $nmbr -eq 4 ]
then
	sudo apt install stacer calibre kdenlive \
	krita libreoffice scribus virtualbox inkscape gimp \
	gparted gufw vlc simplescreenrecorder handbrake \
	audacity git bleachbit
	clear
	echo "Packages Installed - Returning to Menu"
	sleep 3s
	clear
	./update
	exit
elif [ $nmbr -eq 5 ]
then
	clear
	exit
	break
else
	echo "Please select a number from the menu"
	sleep 3s
	clear
fi
./update
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times 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: [Example Script] Install Update Clean etc.

Post by rene »

Forum-wise, you'll want to wrap the script in [code]...[/code] to retain formatting here, but as a more fundamental comment...

The thing you do with

Code: Select all

echo "... - Returning to Menu"
sleep 3s
clear
./update
exit
should really be replaced by an e.g. infinite loop. Your method has (by design, of course) the script relaunch itself but what that means is that you have an ever-growing tree of processes with the first exiting only after all have. Not a huge deal specifically here but generally speaking this is the methodology of a fork-bomb...

I.e., you'd remove the ./update; exit statements from the individual cases and instead wrap the script in e.g.

Code: Select all

while true; do
	...
done
with only an exit (or break, to break out of the infinite loop) as part of the exit-choice.
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

Re: [Example Script] Install Update Clean etc.

Post by roncraig »

I'm new to this and graciously accept any help/advice offered to better myself; thank you for your feedback.

I am trying to wrap my head around the part where you state:
instead wrap the script in e.g.
while true; do
...
done

Specifically, wrap it at which point? Trying to figure out where to properly insert the 'while true; do' and 'done'
OS: Linux Mint 20.3 Una
Kernel: 5.15.0-33
DE: Cinnamon
Mobo: ASRock model: X299 Taichi CLX
CPU: Intel Core i9-10900X
GPU: NVIDIA GeForce RTX 3080
Memory: 64GB
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Example Script] Install Update Clean etc.

Post by rene »

Really around the entire script. With formatting restored:

Code: Select all

#!/bin/bash
while true
do
	clear
	echo "============================="
	echo "= U P D A T E M E N U ="
	echo "============================="
	echo "1. Full System Update/Upgrade"
	echo "2. Clean APT Packages"
	echo "3. Install balenaEtcher"
	echo "4. Install Favorite Software"
	echo "5. Exit/Quit"
	echo "============================="
	read -p "Enter Selection: " nmbr
	if [ $nmbr -eq 1 ]
	then
		sudo apt update
		sudo apt full-upgrade
		clear
		echo "System Updated - Returning to Menu"
	elif [ $nmbr -eq 2 ]
	then
		sudo apt autoclean
		sudo apt autoremove
		clear
		echo "System Cleaned - Returning to Menu"
	elif [ $nmbr -eq 3 ]
	then
		curl -1sLf \
			'https://dl.cloudsmith.io/public/balena/ ... tup.deb.sh' \
			| sudo -E bash
		sudo apt-get update
		sudo apt-get install balena-etcher-electron
		clear
		echo "Etcher Installed - Returning to Menu"
	elif [ $nmbr -eq 4 ]
	then
		sudo apt install stacer calibre kdenlive \
			krita libreoffice scribus virtualbox inkscape gimp \
			gparted gufw vlc simplescreenrecorder handbrake \
			audacity git bleachbit
		clear
		echo "Packages Installed - Returning to Menu"
	elif [ $nmbr -eq 5 ]
	then
		break
	else
		echo "Please select a number from the menu"
	fi
	sleep 3s
done
You may now deal with people objecting to bleachbit being considered Favorite Software...
revmacian
Level 5
Level 5
Posts: 548
Joined: Wed May 27, 2020 1:50 pm
Location: United States

Re: [Example Script] Install Update Clean etc.

Post by revmacian »

rene wrote: Sun Jun 20, 2021 11:18 am Really around the entire script. With formatting restored:

Code: Select all

#!/bin/bash
while true
do
	clear
	echo "============================="
	echo "= U P D A T E M E N U ="
	echo "============================="
	echo "1. Full System Update/Upgrade"
	echo "2. Clean APT Packages"
	echo "3. Install balenaEtcher"
	echo "4. Install Favorite Software"
	echo "5. Exit/Quit"
	echo "============================="
	read -p "Enter Selection: " nmbr
	if [ $nmbr -eq 1 ]
	then
		sudo apt update
		sudo apt full-upgrade
		clear
		echo "System Updated - Returning to Menu"
	elif [ $nmbr -eq 2 ]
	then
		sudo apt autoclean
		sudo apt autoremove
		clear
		echo "System Cleaned - Returning to Menu"
	elif [ $nmbr -eq 3 ]
	then
		curl -1sLf \
			'https://dl.cloudsmith.io/public/balena/ ... tup.deb.sh' \
			| sudo -E bash
		sudo apt-get update
		sudo apt-get install balena-etcher-electron
		clear
		echo "Etcher Installed - Returning to Menu"
	elif [ $nmbr -eq 4 ]
	then
		sudo apt install stacer calibre kdenlive \
			krita libreoffice scribus virtualbox inkscape gimp \
			gparted gufw vlc simplescreenrecorder handbrake \
			audacity git bleachbit
		clear
		echo "Packages Installed - Returning to Menu"
	elif [ $nmbr -eq 5 ]
	then
		break
	else
		echo "Please select a number from the menu"
	fi
	sleep 3s
done
You may now deal with people objecting to bleachbit being considered Favorite Software...
I'm curious as to what happens if the user chooses "no" after the apt prompts. Will the "success" messages still be printed to screen? Seems like more code is needed to process all potential user choices. And, no.. using the --force switches, or similar, is never a good idea in my opinion.. it has the potential to cause problems.
Give a man a fish and you'll feed him for a day. Teach a man to fish and you'll feed him for a lifetime.
US Navy, NEC HM8404
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Example Script] Install Update Clean etc.

Post by rene »

revmacian wrote: Sun Jun 20, 2021 11:28 am I'm curious as to what happens if the user chooses "no" after the apt prompts. Will the "success" messages still be printed to screen?
Certainly. Also, hitting enter at the prompt has the [ $nmbr -eq n ] crap out; ideally has more robust error handling generally. But that's not the point really; in fact possibly somewhat undermines any possible point concerning Joys of Scripting if you will; error handling sucks in any language...
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

Re: [Example Script] Install Update Clean etc.

Post by roncraig »

Thanks Rene!
I'll play/edit the script more and fine tune it. I was just happy it actually worked... now I'll get the cob-webs out of it.
OS: Linux Mint 20.3 Una
Kernel: 5.15.0-33
DE: Cinnamon
Mobo: ASRock model: X299 Taichi CLX
CPU: Intel Core i9-10900X
GPU: NVIDIA GeForce RTX 3080
Memory: 64GB
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

Re: [Example Script] Install Update Clean etc.

Post by roncraig »

Made the changes and it works like a champ! You're the best... thanks :)

This is what it looks like now:

Code: Select all

#!/bin/bash
while true
do
clear
echo "============================="
echo "=        U P D A T E   M E N U        ="
echo "============================="
echo "1. Full System Update/Upgrade"
echo "2. Clean APT Packages"
echo "3. Install balenaEtcher"
echo "4. Install Favorite Software"
echo "5. Exit/Quit"
echo "============================="
read -p "Enter Selection: " nmbr
if [ $nmbr -eq 1 ]
then
	sudo apt update
	sudo apt full-upgrade
	clear
	echo "System Updated - Returning to Menu"
	sleep 3s
	clear
elif [ $nmbr -eq 2 ]
then
	sudo apt autoclean
	sudo apt autoremove
	clear
	echo "System Cleaned - Returning to Menu"
	sleep 3s
	clear
elif [ $nmbr -eq 3 ]
then
	curl -1sLf \
	'https://dl.cloudsmith.io/public/balena/etcher/setup.deb.sh' \
	| sudo -E bash
	sudo apt-get update
	sudo apt-get install balena-etcher-electron
	clear
	echo "Etcher Installed - Returning to Menu"
	sleep 3s
	clear
elif [ $nmbr -eq 4 ]
then
	sudo apt install -yy stacer calibre kdenlive \
	krita libreoffice scribus virtualbox inkscape gimp \
	gparted gufw vlc simplescreenrecorder handbrake \
	audacity git bleachbit ttf-mscorefonts-installer
	clear
	echo "Packages Installed - Returning to Menu"
	sleep 3s
	clear
elif [ $nmbr -eq 5 ]
then
	clear
	exit
	break
else
	echo "Please select a number from the menu"
	sleep 3s
	clear
fi
done
OS: Linux Mint 20.3 Una
Kernel: 5.15.0-33
DE: Cinnamon
Mobo: ASRock model: X299 Taichi CLX
CPU: Intel Core i9-10900X
GPU: NVIDIA GeForce RTX 3080
Memory: 64GB
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Example Script] Install Update Clean etc.

Post by rene »

Looking good (-ish; the body of the while is not indented in your version; hard to immediately see structure due to that). Random remaining remarks for you or other readers...

I had split out the sleep 3s to after the if since it's shared over all cases anyway, and had removed the clear from after it due to the the code now returning to the top of the while loop; the first statement of that is already also clear.

While your use of exit rather than (just) break in the quit-case is fine, after exit nothing is executed so may as well delete the subsequent break. I myself used break instead of exit to have it just break out of the while --- which in this case means it exits immediately as well so here it doesn't functionally matter.

Tend to myself also indent continued lines.

A final style-only remark. The by you used style

Code: Select all

if ...
then
	...
fi
and my copying of it for

Code: Select all

while true
do
	...
done
can be said to be "GNU style", at least in the sense of of being similarly "vertically verbose". You'll in practice more frequently encounter the style

Code: Select all

if ...; then
	...
fi
and

Code: Select all

while true; do
	...
done
Fitting as much code on one page/screen as possible helps overview, even though in the end it's definitely a personal style issue.
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

Re: [Example Script] Install Update Clean etc.

Post by roncraig »

You sure have taught me a lot today :)

So... like this?:

Code: Select all

#!/bin/bash
while true; do
	clear
	echo "============================="
	echo "=   U P D A T E   M E N U   ="
	echo "============================="
	echo "1. Full System Update/Upgrade"
	echo "2. Clean and Fix APT Packages"
	echo "3. Install balenaEtcher"
	echo "4. Install Favorite Software"
	echo "5. Exit/Quit"
	echo "============================="
	read -p "Enter Selection: " nmbr
	if [ $nmbr -eq 1 ]; then
		sudo apt update
		sudo apt full-upgrade
		clear
		echo "System Updated - Returning to Menu"
		sleep 3s
	elif [ $nmbr -eq 2 ]; then
		sudo apt install -f
		sudo apt autoclean
		sudo apt autoremove
		clear
		echo "System Cleaned - Returning to Menu"
		sleep 3s
	elif [ $nmbr -eq 3 ]; then
		curl -1sLf \
		'https://dl.cloudsmith.io/public/balena/etcher/setup.deb.sh' \
		| sudo -E bash
		sudo apt-get update
		sudo apt-get install balena-etcher-electron
		clear
		echo "Etcher Installed - Returning to Menu"
		sleep 3s
	elif [ $nmbr -eq 4 ]; then
		sudo apt install -yy stacer calibre kdenlive \
		krita libreoffice scribus virtualbox inkscape gimp \
		gparted gufw vlc simplescreenrecorder handbrake \
		audacity git bleachbit ttf-mscorefonts-installer
		clear
		echo "Packages Installed - Returning to Menu"
		sleep 3s
	elif [ $nmbr -eq 5 ]; then
		clear
		exit
		break
	else
		echo "Please select a number from the menu"
		sleep 3s
	fi
done
OS: Linux Mint 20.3 Una
Kernel: 5.15.0-33
DE: Cinnamon
Mobo: ASRock model: X299 Taichi CLX
CPU: Intel Core i9-10900X
GPU: NVIDIA GeForce RTX 3080
Memory: 64GB
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

Re: [Example Script] Install Update Clean etc.

Post by roncraig »

On a side-note, I also made an alias so I can run it where ever I am in the terminal just by typing 'update':

alias update='cd /home/rcraig/scripts && ./update'

... I just had to place that alias in my .bashrc file so it would stick and be available between boots
OS: Linux Mint 20.3 Una
Kernel: 5.15.0-33
DE: Cinnamon
Mobo: ASRock model: X299 Taichi CLX
CPU: Intel Core i9-10900X
GPU: NVIDIA GeForce RTX 3080
Memory: 64GB
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Example Script] Install Update Clean etc.

Post by rene »

Much better, but I'd also as said still remove the sleep 3s from the individual cases and have it just once on the line below the closing fi, i.e., as the last statement of the while block. And delete either exit or break from the quit-case (both are fine; I'd myself slightly prefer leaving only break, which breaks out of the while, i.e., continues after done, i.e., in this case, also exits but in a more standard manner "at the end of the script").

It's all not very important; the way it actually works will be the exact same :)

As to the alias: note that if you create a directory "bin" in your home directory (and log out of the system and back in) that you can also place the "update" script there and have it be executable from anywhere: ~/bin is if it exists added to your user's PATH automatically.
User avatar
jimallyn
Level 19
Level 19
Posts: 9075
Joined: Thu Jun 05, 2014 7:34 pm
Location: Wenatchee, WA USA

Re: [Example Script] Install Update Clean etc.

Post by jimallyn »

Darn good for first time!
“If the government were coming for your TVs and cars, then you'd be upset. But, as it is, they're only coming for your sons.” - Daniel Berrigan
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

Re: [Example Script] Install Update Clean etc.

Post by roncraig »

Rene, you keep giving me great ideas to work on and thing about; let me make those tweaks/adjustment and I'll repost the code for final review.

Jimallyn, thank you for the compliment :) I find all this so fascinating/interesting and constructive. It must be what a carpenter feels like with his tools and a stack of lumber :lol:
OS: Linux Mint 20.3 Una
Kernel: 5.15.0-33
DE: Cinnamon
Mobo: ASRock model: X299 Taichi CLX
CPU: Intel Core i9-10900X
GPU: NVIDIA GeForce RTX 3080
Memory: 64GB
roncraig
Level 1
Level 1
Posts: 44
Joined: Tue Jul 14, 2015 5:37 pm
Location: Ohio

Re: [Example Script] Install Update Clean etc.

Post by roncraig »

Ok, I think it is now ready for final review!
I could not have gotten this far without your advice Rene. This works so much smoother now.

It is my hope that other people will find this useful and they can copy/edit it to suit their needs.

Here it is:

Code: Select all

#!/bin/bash
while true; do
	clear
	echo "============================="
	echo "=   U P D A T E   M E N U   ="
	echo "============================="
	echo "1. Full System Update/Upgrade"
	echo "2. Clean and Fix APT Packages"
	echo "3. Install balenaEtcher"
	echo "4. Install Favorite Software"
	echo "5. Exit/Quit"
	echo "============================="
	read -p "Enter Selection: " nmbr
	if [ $nmbr -eq 1 ]; then
		sudo apt update
		sudo apt full-upgrade
		clear
		echo "Full system update completed"
	elif [ $nmbr -eq 2 ]; then
		sudo apt install -f
		sudo apt autoclean
		sudo apt autoremove
		clear
		echo "APT packages fixed and cleaned"
	elif [ $nmbr -eq 3 ]; then
		curl -1sLf \
		'https://dl.cloudsmith.io/public/balena/etcher/setup.deb.sh' \
		| sudo -E bash
		sudo apt-get update
		sudo apt-get install balena-etcher-electron
		clear
		echo "Etcher installed"
	elif [ $nmbr -eq 4 ]; then
		sudo apt install -yy stacer calibre kdenlive \
		krita libreoffice scribus virtualbox inkscape gimp \
		gparted gufw vlc simplescreenrecorder handbrake \
		audacity git bleachbit ttf-mscorefonts-installer
		clear
		echo "Favorite packages installed"
	elif [ $nmbr -eq 5 ]; then
		clear
		break
	else
		clear
		echo "Please select a number from the menu"
	fi
    sleep 3s
done
OS: Linux Mint 20.3 Una
Kernel: 5.15.0-33
DE: Cinnamon
Mobo: ASRock model: X299 Taichi CLX
CPU: Intel Core i9-10900X
GPU: NVIDIA GeForce RTX 3080
Memory: 64GB
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Example Script] Install Update Clean etc.

Post by rene »

Yap; looking perfect. Sure many will be able to get inspiration from it :)
llovepancakes
Level 1
Level 1
Posts: 34
Joined: Sun Jun 21, 2020 11:02 pm

Re: [Example Script] Install Update Clean etc.

Post by llovepancakes »

helpful posts.

how do you run it in terminal without cd in the directory its stored?

or better question, can we see a screenshot of your script folder? interesting to see what people have.
i currently have 1 txt and commands i keep copy paste...... :shock:
ajgringo619

Re: [Example Script] Install Update Clean etc.

Post by ajgringo619 »

Once you get more comfortable with scripting and creating menus, check out dialog (console-based) and zenity (GUI-based). Both are already installed on Mint.

You'll be surprised how much you can do with simple scripts.
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: [Example Script] Install Update Clean etc.

Post by AndyMH »

And there is yad, does the same as zenity but more. Needs to be installed from software manager.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [Example Script] Install Update Clean etc.

Post by Termy »

AndyMH wrote: Fri Jul 09, 2021 5:56 am And there is yad, does the same as zenity but more.
I can barely stomach working with GTK in PERL, but I think I'd rather use that than faff about with YAD or Zenity! I'm trying right now to get something usable out of YAD, but it's not my idea of a good time. Working with dialog(1) and whiptail(1) seems to be similarly clunky, but a bit easier to handle. YMMV, of course. I'm pretty stuck in my terminal ways. :lol:

Fun fact: YAD is a fork of Zenity.
Last edited by Termy on Fri Jul 09, 2021 12:13 pm, edited 1 time in total.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”