0

I am writing my own script to install my Arch Linux. I would like to have few options to change/configure every time I reinstall the system or use my script in another device.
I would like to configure the script so that: If I choose BTRFS, the relevent commands of formating, subvolume creation as well as mounting will be executed.
Likewise, if I choose Gnome for Desktop environment, the script should install Gnome AND the relevant display manager.

I can partially achieve this by writing the variables at the top of my script. I will then comment or uncomment as required before I run the script. However, if I want the script to have more options, there will be too many variables to define.

Currently the script looks like the below:

DISK="nvme0n1"
#DISK="sda"
UEFIPARTITION="nvme0n1p1"
#UEFIPARTITION="sda1"
SYSTEMPARTITION="nvme0n1p2"
#SYSTEMPARTITION="sda2"

and then somewhere in the script there will be:

gdisk /dev/$DISK
.
.
.
mkfs.vfat -n BOOT /dev/$UEFIPARTITION
mkfs.btrfs -L SYSTEM /dev/$SYSTEMPARTITION

It would be easier to just set one variable which is the DISK to either sdX or nvme0n1. Then accordingly I can have the relevant commands to be executed for each.
I am a beginner in writing scripts.
What is the easiest way to achieve what I explained. I am happy if someone can explain another/better way of achieving the above.

Muzzamil
  • 3
  • 2
  • Remember to always quote your variables and also avoid using CAPS for variable names in shell scripts. By convention, global environment variables are in caps, so if your variables are also capitalized, this can lead to naming collisions and hard to debug issues. – terdon Oct 30 '22 at 15:47

1 Answers1

0

There are several ways to do options in scripts

  1. Traverse arguments array
# define a named array for convenience from script arguments array
ARG=( "${@}" )

for i in ${ARG[@]}; do echo $i done

  1. Use getopt command (man 1 getopt)
A=0
while getopts "ab:h" option; do
   case $option in
      a) # a key -a was given
         A=1
         ;;
      b) # The colon in definition means it requires argument
         BARG = $optarg
         ;;
      h) # display Help
         echo Help text
         exit;;
      *) # any other argumnet
         echo Unknown option $option
         exit;;
   esac
done
White Owl
  • 5,129
  • Could you please explain the above further? Perhaps with one of the examples I gave in the question like desktop environments or disk layout. – Muzzamil Oct 30 '22 at 13:32