Score:-1

Configuring a script with multiple options

ru flag

I am writing a script for my personal use and learning. I would like to have few options to change/configure at the top of my script which I can change every time I execute the script.
Below is just a random example of what I want to achieve.
I would like to configure the script so that: If I define Backup="timeshift" then the relevant commands should be executed for e.g.

Command 1 #install timeshift
Command 2 #configure timeshit
Command 3 #make a backup with timeshift

But if I define Backup="snapper" then it should install the relevant commands NOT the timeshift ones.

Command 4 #install snapper
Command 5 #configure snapper
Command 6 #make a backup with snapper

I also need the script at certain parts to either execute some commands or do nothing. For e.g. network_backup="true" then:

Command 7 # upload to the cloud storage

But if I defined network_backup="false" at the top of the script then nothing should be executed.

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.

Esther avatar
es flag
you will need to learn `if` statements in bash. That's all.
Score:0
ng flag

You know the problem you want to solve. That's the first step. And you also know what success looks like to you; that's the second. What's missing is how to think about the route between what you want and what you've got.

Since you're learning, I'll suggest what I think might be the optimal way to do it, and it's up to you to write the code.

For each backup program you want to be able to run, write a bash function that runs the commands to:

  1. Determine if the program is installed;
  2. Install it if it isn't;
  3. Determine if the program is configured as you want it to be;
  4. Configure it if it isn't; and
  5. Return an exit code letting you how things went.

Then you might show the user a list of programs (a menu) and have them choose one. This will call the appropriate function.

After that, either the desired program installed and configured or it's not, and you know which one is the case. If it failed, you want to quit so you can investigate; if it succeeded, you'll run the program. Please grab the exit code from this as well.

As far as the rest, the "execute commands or do nothing", it's simple enough to set up one or more "yes/no" prompts, taking the backup program's exit code into consideration.

(Now, if it were I, I'd write a separate script for each program. But I prefer simplicity and I don't mind maintaining two or three simple scripts instead of one more complicated one.)

ng flag
To save you a little searching, here's how I like to test if a program is installed: `command -v timeshift > /dev/null 2>&1 && echo "Installed" || echo "Not Installed"`
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.