Examples
Read the manual page for bash!
Even if you don't fully understand the material in front of you, it is good to give it a once-over just so you are aware it exists. Just read the page, and know it exists. It is always readily available to you and quick to search through. If needed, check out my not-so-brief Introduction to Manual Pages to learn how to reference these manual pages more efficiently.
Creating Scripts
Bash scripting is much like interacting with the bash terminal - the similarity can be easily seen in how we would split a bash command to multiple lines...
kapak@base:~$ l\
> s -la
total 76
drwxr-xr-x 9 kapak kapak 4096 Jul 28 01:24 .
drwxr-xr-x 3 root root 4096 Jul 6 09:49 ..
-rw------- 1 kapak kapak 5423 Jul 20 18:10 .bash_history
-rw-r--r-- 1 kapak kapak 220 Jul 6 09:49 .bash_logout
-rw-r--r-- 1 kapak kapak 3771 Jul 6 09:49 .bashrc
...( Reduced Output ) ...
kapak@base:~$ ls -la
total 76
drwxr-xr-x 9 kapak kapak 4096 Jul 28 01:24 .
drwxr-xr-x 3 root root 4096 Jul 6 09:49 ..
-rw------- 1 kapak kapak 5423 Jul 20 18:10 .bash_history
-rw-r--r-- 1 kapak kapak 220 Jul 6 09:49 .bash_logout
-rw-r--r-- 1 kapak kapak 3771 Jul 6 09:49 .bashrc
...( Reduced Output ) ...
In a bash script, we would handle splitting ls -la
across multiple lines much the same. Create the file below, name it test.sh -
#/bin/bash
ls -la
l\
s -la
Now make the file executable and run the script, you should see the output of ls -la
twice, since this script is a simple example of splitting commands across lines.
# Make the script executable
sudo chmod a+x test.sh
# Run the script
./test.sh
Printf Formatting
This is just one of many commands in bash, but you will use it a lot so getting to know the syntax well will make your life a lot easier.
#!/bin/bash
## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
## A custom bash script to configure vim with my preferred settings ##
## Run as user with sudo within directory to store / stash .vimrc configs ##
###############################################################################
# Example of easy colorization using printf
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
UNDERLINE=$(tput smul)
NORMAL=$(tput sgr0)
# Script Reduced, lines removed
# Example of creating an array of strings to be passed to printf
welcome=( "\nEnter 1 to configure vim with the Klips repository, any other value to exit." \
"The up-to-date .vimrc config can be found here: https://github.com/shaunrd0/klips/tree/master/configs" \
"${RED}Configuring Vim with this tool will update / upgrade your packages${NORMAL}\n\n")
# Create a printf format and pass the entire array to it
# Will iterate through array, filling format provided with array contents
# Useful for printing / formatting lists, instructions, etc
printf '%b\n' "${welcome[@]}"
read cChoice
# Script Reduced, lines removed
Using the above method, you could easily create a single array containing multiple responses to related paths the script could take for a related option, and refer to the appropriate index of the array directly, instead of passing all of the contents of the array to the same format.
For more advanced formatting, read the below script carefully, and you will have a basic understanding of how printf can be used dynamically within scripts to provide consistent formatting.
#/bin/bash
divider===============================
divider=$divider$divider
header="\n %-10s %8s %10s %11s\n"
format=" %-10s %08d %10s %11.2f\n"
width=43
printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE"
printf "%$width.${width}s\n" "$divider"
printf "$format" \
Triangle 13 red 20 \
Oval 204449 "dark blue" 65.656 \
Square 3145 orange .7
# https://linuxconfig.org/bash-printf-syntax-basics-with-examples
Loops
While loop using conditionals within bash to automate cmake builds -
#!/bin/bash
## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
## A custom bash script for building cmake projects. ##
## Intended to be ran in root directory of the project alongside CMakeLists ##
###############################################################################
# Infinite while loop - break on conditions
while true
do
printf "\nEnter 1 to build, 2 to cleanup previous build, 0 to exit.\n"
read bChoice
# Build loop
# If input read is == 1
if [ $bChoice -eq 1 ]
then
mkdir build
# Move to a different directory within a subshell and build the project
# The '(' and ')' here preserves our working directory
(cd build && cmake .. && cmake --build .)
fi
# Clean-up loop
# If input read is == 2
if [ $bChoice -eq 2 ]
then
printf "test\n"
rm -Rv build/*
fi
# Exit loops, all other input -
# If input read is >= 3, exit
if [ $bChoice -ge 3 ]
then
break
fi
# If input read is <= 0, exit
if [ $bChoice -le 0 ]
then
break
fi
# Bash will print an error if symbol or character input
donedone
Subshells
Sometimes, you may want to stay in your active directory but perform some action in another. To do this, we can do something in a subshell (a background shell) -
$ (cd /var/log && cp -- *.log ~/Desktop)
Examples
Check out my snippet repository for more up-to-date scripts, configurations - /shaunrd0/klips
Basic script example of adding a user to a Linux system. This script uses parameters, basic formatting, and can either be ran as root or as a user without sudo, depending on the need. If needed, see the Knoats Book Adding Linux Users for more explanation on the below.
#!/bin/bash
## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
## A custom bash script for creating new linux users. ##
## Syntax: ./adduser.sh <username> <userID> ##
###############################################################################
if [ "$#" -ne 2 ]; then
printf "Illegal number of parameters."
printf "\nUsage: sudo ./adduser.sh <username> <groupid>"
printf "\n\nAvailable groupd IDs:"
printf "\n60001......61183 Unused | 65520...............65533 Unused"
printf "\n65536.....524287 Unused | 1879048191.....2147483647 Unused\n"
exit
fi
sudo adduser $1 --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --uid $2
printf "\nEnter 1 if $1 should have sudo privileges. Any other value will continue and make no changes\n"
read choice
if [ $choice -eq 1 ] ; then
printf "\nConfiguring sudo for $1...\n"
sudo usermod -G sudo $1
fi
printf "\nEnter 1 to set a password for $1, any other value will exit with no password set\n"
read choice
if [ $choice -eq 1 ] ; then
printf "\nChanging password for $1...\n"
sudo passwd $1
fi