[Solved] Why use a bash script?

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
Whitehorse2018

[Solved] Why use a bash script?

Post by Whitehorse2018 »

So I’m totally new to Linux but I really want to learn as much as possible, especially with regards bash and terminal commands.

I’d like to try and find my way along to writing a bash script file, but my biggest question is why might I want to write a bash script file?!?

What is it for, and what kind of things can I do with it?

Apologies now if that’s such a basic question!
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.
mediclaser
Level 4
Level 4
Posts: 492
Joined: Tue Mar 20, 2018 2:28 pm

Re: Why use a bash script?

Post by mediclaser »

It's useful when you find yourself repeating the same sequence of terminal commands regularly, and you want to automate them into a single command. :)
If you're looking for a greener Linux pasture, you won't find any that is greener than Linux Mint. ;)
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Why use a bash script?

Post by Flemur »

Whitehorse2018 wrote: Thu Dec 27, 2018 6:55 pmWhat is it for, and what kind of things can I do with it?
I have a whole bunch of simple scripts; they mostly save some typing for things I do regularly. I usually start them with a capital letter so I don't duplicate some other function/command.

Find file names using one or two strings -
cat ~/bin/Find:

Code: Select all

#!/bin/bash
# 
if [ $# -lt 1 ]
then
  echo Usage: `basename $0` pattern
  exit 1
fi
if [ $# -lt 2 ]
then
   find -iname "*$1*"
   exit 0
fi
find -iname "*$1*" | grep $2
exit 0

Code: Select all

$ Find the
./.local/share/rofi/themes
...
./.config/polybar/modules/ethernet.conf
...
./.fluxbox/styles/BlackWhite/theme.cfg
$
$ Find the mod
./.config/polybar/modules/ethernet.conf
$ 
Show all the executables in your $PATH that contain a string:

$ cat bin/W

Code: Select all

#!/bin/bash
#
if [ $# -lt 1 ]
then
  echo Usage: `basename $0` pattern 
  exit 1
fi 
echo $PATH | sed 's/^/ls -A /' | sed 's/:/ |grep -i '$1'; ls -A /g' | sed 's/$/ |grep -i '$1'/' | bash | sort -u
exit 0

Code: Select all

$ W edit
desktop-file-edit
edit
...
vedit
xfce4-settings-editor

Code: Select all

$ W xfce4
xfce4-about
xfce4-accessibility-settings
...
xfce4-terminal
$ 
Show basic boot-up time:
$ cat bin/stime

Code: Select all

echo "Running: systemd-analyze"
systemd-analyze 
$ stime

Code: Select all

Running: systemd-analyze
Startup finished in 1.374s (kernel) + 1.857s (userspace) = 3.232s 
graphical.target reached after 1.855s in userspace
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
slipstick
Level 6
Level 6
Posts: 1071
Joined: Sun Oct 21, 2012 9:56 pm
Location: Somewhere on the /LL0 scale

Re: Why use a bash script?

Post by slipstick »

To automate the process of verifying and authenticating a newly downloaded Linux Mint .iso file:

Code: Select all

#!/bin/bash

#--------------------------------------------------------------------------------------------------------------------
# Save this file as "LM_iso_verify.sh" in a directory on the command PATH, such as ~/bin or ~/.local/bin, and
#  make it executable
#
#          ********  To check the integrity of the downloaded LMxx.x .iso file: ********

#  * Download or move your Mint .iso file to your Downloads folder (or a folder under Downloads, such as
#    ~/Downloads/LM_isos).

#  * Open a terminal window and change directory (cd) to the folder containing the .iso file.

#  * Run the script following this example (substitute the correct name of the .iso file):
#      EXAMPLE:   LM_iso_verify.sh  linuxmint-19-cinnamon-64bit.iso
#--------------------------------------------------------------------------------------------------------------------



# Checking for one parameter on the command line (the .iso file name)
if [ $# -ne 1 ]; then
	echo "Usage: $(basename "$0") linuxmint-xx.x-correct-filename.iso"
	exit 1
fi

# Checking if that file exists in the working directory and is readable
if [ ! -r "$1" ]; then
	echo "File $1 does not exist or is not readable"
	exit 1
fi

# Importing the signing key (LM18 and higher)
gpg --keyserver keyserver.ubuntu.com --recv-key "27DE B156 44C6 B3CF 3BD7 D291 300F 846B A25B AE09"


# *** BE SURE THE CORRECT VERSION IS SPECIFIED IN THE "wget" LINES BELOW ! ***
# This is for LM19 versions - modify these wget lines as needed for other versions

# Downloading the sha256 sum.txt and sum.txt.gpg files

# If heanet.ie site isn't working, comment out these two lines and uncomment the wget lines for the alternate site
#    or use another alternate of your choice.
wget https://ftp.heanet.ie/mirrors/linuxmint.com/stable/19/sha256sum.txt
wget https://ftp.heanet.ie/mirrors/linuxmint.com/stable/19/sha256sum.txt.gpg

# University of Oklahoma mirror - an alternate site if above heanet.ie isn't working
#wget http://reflection.oss.ou.edu/linuxmint/isos/linuxmint.com/stable/19/sha256sum.txt
#wget http://reflection.oss.ou.edu/linuxmint/isos/linuxmint.com/stable/19/sha256sum.txt.gpg




# Verifying the signature on the sha256 sum text file
gpg --verify sha256sum.txt.gpg sha256sum.txt
echo "It should report that the signature is Good, $USER."
echo "You can ignore any warning about ...not certified..."
echo "..."

# comparing the sha256 sum of your ISO image and the original Mint sha256 sum
echo "Calculating the sha256 sum for $1 and comparing it to the downloaded signed sha256 sum"
echo "Be patient, $USER. I am not that good at math"
echo "..."

# this line works if Gnu CoreUtils version 8.25 or later is installed (LM18 ? or later)
sha256sum --check --ignore-missing sha256sum.txt

# or if an earlier version of CoreUtils is installed, then
#  before running this script, comment the above "sha256sum ....." line and uncomment the following line:
# sha256sum -b *.iso
#  you will then need to manually compare the sha256 sum generated to the appropriate line in the sha256sum.txt file


echo "Done."

To make a backup of my TimeShift partition on my internal HDD to an external USB HDD while preserving the hard-link structure:
EDIT: If you use this one, it's probably best to disable TS snapshots before running it, and re-enable after it completes (can take 10 to 20 minutes to run). I've been meaning to incorporate that into the script (if I can find a way to do it) so it will automatically disable and enable snapshots, but haven't gotten around to it yet.

Code: Select all

#!/bin/bash
#update timeshift backup on external drive from snapshots on internal HD

sudo rsync -aAuHXvis --progress --delete --numeric-ids /media/root/LM18_timeshift/ /media/steve/TS_Backups/LM18.3_timeshift_backup | tee ~/timeshift_backup_log

echo "Finished updating timeshift backup"

sleep 10
exit 0


to learn terminal commands, I recommend this book - free pdf download available here:
http://linuxcommand.org/tlcl.php
Last edited by slipstick on Fri Dec 28, 2018 3:38 am, edited 1 time in total.
In theory, theory and practice are the same. In practice, they ain't.
redlined

Re: Why use a bash script?

Post by redlined »

+1 for the book! and other sites I recommended on your intro thread. start slow and easy, what's the rush? :))
(in no particular order of preference)

http://linuxcommand.org/index.php

https://linuxjourney.com/

https://ryanstutorials.net/linuxtutorial/cheatsheet.php
...
edit to add: I forgot to mention, one such local expert has a blog for easy linux tips, very useful for setting things up on new install, common issues, etc and keeping it simple to understand for us new to linux folks:
https://easylinuxtipsproject.blogspot.com
and thank you Flemur and slipstick! wow, gems for real, thanks for sharing them diamonds!
Whitehorse2018

Re: Why use a bash script?

Post by Whitehorse2018 »

Thank you guys, that answered that one!
User avatar
jimallyn
Level 19
Level 19
Posts: 9075
Joined: Thu Jun 05, 2014 7:34 pm
Location: Wenatchee, WA USA

Re: [Solved] Why use a bash script?

Post by jimallyn »

bash scripts can be very powerful. It happens fairly regularly that somebody will ask how to do some uncommon function, and one of the geeks will write them a script or even a one-liner. I use the GUI for most stuff, but there's some stuff that bash is simply the best and easiest way to do it. I was having some problems with my internet connection and wound up using a script to repeatedly ping a couple of URLs, run a check at speedtest.net, and output the results to a text file. You couldn't buy a program to do that, but you can easily write a script to do it.
“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
Locked

Return to “Scripts & Bash”