I started self-learning Bash scripting yesterday, and I finished my first successful "advance" script.
Goal
To create an interactive step-by-step script to that allowed customizing various parts of the process of creating permanent swapfiles. I based my idea off of Ask Ubuntu Answer's #1 and #2.
Background
While creating it, I extended ideas such as allowing exiting from the script at any time and formatting the output.
You will see that I use different styles throughout the code (i.e. case vs if-elif-else, etc...). That deals with me learning as I coded, and having issues trying to get the syntax right while learning. I want to consolidate my different styles - probably into cases.
Additional Concerns I have
I know that all of you will suggest any issues that you see, and I will be happy about that; however, I am curious about a few things.
- Is this code portable? [Edit] Mostly anyone with a Linux Distro should be able to use this script.
- I put a lot of effort into formatting the output and making it look easy to read. I tried to separate the output of my code vs the output of other commands (i.e. adding
>>
and>>>>
). Is it good enough? Would you suggest anything else? etc?
Possible Extensions
The code works as intended; however, I do plan on extending it in the future. These could affect possible responses - some response might make these harder or easier to implement, or they might have no effect at all. Anyways, if I extend the code later on to accommodate more use-cases, these are what I believe I could do in the future:
- Allow a non-interactive script mode that uses arguments for customizations
- Allow single/batch removal of swapfiles.
- Allow easier testing of each pathway of the interactive script
- Allow color and bold support.
Code
GitHub Repo at ciscorucinski/Swapfile-Utils
For unmodified code, check out the Protected Branch at
1_200606_StackExchange-CodeReview
Note: Only the variable declaration of
help_text
on the GitHub link is different due to a mistake on my side. No escaped tabs and newlines in GitHubFor updated code, check out the Development Branch at
develop
#!/bin/bash
help_text="/n>> Documentation:\n\n\t
Usage: $(basename "$0") [-h]\n\t
Creates a new permanent swapfile\n\n\t
where:\n\t\t
-h displays help text"
ask_for_size_allocation="
>>>> Amount to allocate for new swapfile? (default: 4G) : "
ask_for_swapfile_name="
>>>> Filename for new swapfile? (default: '/swapfile') : "
line="# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
shopt -s extglob
case $1 in
?(-)@(-h|-help) )
echo -e help_text
;;
esac
retry=true
while $retry; do
echo -e -n ">>>> Are you sure you want to create a new swapfile? (Y / N):"
read yes_no
case $yes_no in
[Yy]|[Yy][Ee][Ss] )
echo -e "\n$line"
echo -e "Current Swapfiles:\n"
sudo swapon -s
echo -e "$line"
retry=false
;;
[Nn]|[Nn][Oo]|[Qq][Uu][Ii][Tt] )
echo -e ">> Exiting..."
exit 0
;;
* )
echo -e ">> Error: invalid response\n"
retry=true
esac
done
echo -e ""
echo -e ">> Step 1: Size Allocation"
echo -e -n $ask_for_size_allocation
read swap_size
if [ -z "${swap_size}" ]; then
swap_size="4G"
elif [[ $swap_size =~ [1-9][0-9]*[mMgG] ]]; then
:
elif [[ $swap_size =~ [Qq][Uu][Ii][Tt] ]]; then
echo -e ">> Exiting..."
exit 0
else
echo -e ">> Invalid Size: ${swap_size^^}. Exiting..."
exit 1
fi
echo -e ""
echo -e ">> Step 2: File Name"
echo -e -n $ask_for_swapfile_name
read swap_name
if [ -z "${swap_name}" ]; then
swap_name="/swapfile"
elif [[ $swap_size =~ [Qq][Uu][Ii][Tt] ]]; then
echo -e ">> Exiting..."
exit 0
elif [[ $swap_name =~ [/]+([0-9a-zA-Z]|[_-]) ]]; then
:
elif [[ $swap_name =~ [+([0-9a-zA-Z]|[_-])] ]]; then
swap_name="/$swap_name"
else
echo -e ">> Invalid Pattern: $swap_name. Exiting..."
exit 1
fi
echo -e ""
echo -e -n ">>>> Continue? '$swap_name' (${swap_size^^}) will be created. (Y / N):"
read yes_no
case $yes_no in
[Yy]|[Yy][Ee][Ss] )
echo -e""
echo -e ">> 1. Creating swapfile..."
echo -e ""
echo -e "$line"
sudo fallocate -l $swap_size $swap_name
sudo chmod 600 $swap_name
sudo mkswap $swap_name
echo -e "$line"
echo -e ""
echo -e ">> 2. Enabling swapfile..."
sudo swapon $swap_name
echo -e ">> 3. Swapfile added."
echo -e ""
echo -e "$line"
echo -e "Current Swapfiles:"
echo -e ""
sudo swapon -s
echo -e "$line"
echo -e ""
;;
[Qq][Uu][Ii][Tt]|[Nn]|[Nn][Oo]|[*])
echo -e ">> Exiting..."
exit 0
;;
esac
echo -e -n ">>>> Make swapfile permanent? (Y / N):"
read yes_no
case $yes_no in
[Yy]|[Yy][Ee][Ss] )
echo -e "$swap_name none swap sw 0 0" | sudo tee -a /etc/fstab > /dev/null
echo -e ""
echo -e ">> 4. Created permanent swapfile. Modified '/etc/fstab'"
echo -e -n ">>>> Do you want to view '/etc/fstab?' (Y / N):"
read yes_no
case $yes_no in
[Yy]|[Yy][Ee][Ss] )
lenght=${#swap_name}
echo -e ""
echo -e "$line"
cat /etc/fstab
echo -e "$line"
;;
*)
echo -e ">> Exiting..."
exit 0
;;
esac
;;
[Qq][Uu][Ii][Tt] )
echo -e ">> Exiting..."
exit 0
;;
*)
echo -e ">> 4. Created temp swapfile."
echo -e ">> Exiting..."
exit 0
;;
esac
shopt -u extglob
Output
$ bash new_swapfile.sh
>>>> Are you sure you want to create a new swapfile? (Y / N):y
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Current Swapfiles:
[sudo] password for ciscorucinski:
Filename Type Size Used Priority
/swapfile_ext3 file 262140 238384 -5
/swapfile_ext7 file 131068 106376 -3
/swapfile file 8388604 476148 -6
/swapfile_ext6 file 131068 68840 -4
/swapfile_ext5 file 131068 115304 -2
/dev/dm-1 partition 1003516 0 -7
/swapfile_ext8 file 131068 0 -8
/swapfile_ext9 file 131068 0 -9
/swapfile_ext10 file 131068 0 -10
/swapfile_ext11 file 131068 0 -11
/swapfile_ext12 file 131068 0 -12
/swapfile_ext13 file 131068 0 -13
/swapfile_ext file 524284 0 -14
/swapfile_ext14 file 131068 0 -15
/swapfile_ext15 file 131068 0 -16
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
>> Step 1: Size Allocation
>>>> Amount to allocate for new swapfile? (default: 4G) :128m
>> Step 2: File Name
>>>> Filename for new swapfile? (default: '/swapfile') :/swapfile_ext16
>>>> Continue? '/swapfile_ext16' (128M) will be created. (Y / N):y
>> 1. Creating swapfile...
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Setting up swapspace version 1, size = 128 MiB (134213632 bytes)
no label, UUID=94e452f1-b252-4ea2-8d5f-e23634e34081
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
>> 2. Enabling swapfile...
>> 3. Swapfile added.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Current Swapfiles:
Filename Type Size Used Priority
/swapfile_ext3 file 262140 238384 -5
/swapfile_ext7 file 131068 106376 -3
/swapfile file 8388604 476148 -6
/swapfile_ext6 file 131068 68840 -4
/swapfile_ext5 file 131068 115304 -2
/dev/dm-1 partition 1003516 0 -7
/swapfile_ext8 file 131068 0 -8
/swapfile_ext9 file 131068 0 -9
/swapfile_ext10 file 131068 0 -10
/swapfile_ext11 file 131068 0 -11
/swapfile_ext12 file 131068 0 -12
/swapfile_ext13 file 131068 0 -13
/swapfile_ext file 524284 0 -14
/swapfile_ext14 file 131068 0 -15
/swapfile_ext15 file 131068 0 -16
/swapfile_ext16 file 131068 0 -17
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
>>>> Make swapfile permanent? (Y / N):y
>> 4. Created permanent swapfile. Modified '/etc/fstab'
>>>> Do you want to view '/etc/fstab?' (Y / N):y
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/ubuntu--vg-root / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/nvme0n1p1 during installation
UUID=D6C3-F1E1 /boot/efi vfat umask=0077 0 1
/dev/mapper/ubuntu--vg-swap_1 none swap sw 0 0
/swapfile none swap sw 0 0
/swapfile_ext3 none swap sw 0 0
/swapfile_ext5 none swap sw 0 0
/swapfile_ext6 none swap sw 0 0
/swapfile_ext7 none swap sw 0 0
/swapfile_ext8 none swap sw 0 0
/swapfile_ext9 none swap sw 0 0
/swapfile_ext10 none swap sw 0 0
/swapfile_ext11 none swap sw 0 0
/swapfile_ext13 none swap sw 0 0
/swapfile_ext14 none swap sw 0 0
/swapfile_ext15 none swap sw 0 0
/swapfile_ext16 none swap sw 0 0
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #