I use Ubuntu 16.04 and I have a localization file, a file that executes many files, that I got after downloading my own GitHub project to my machine.
This file contains a Bash script, and is named localize.sh
. I run it in a subsession via ~/${repo}/localize.sh
.
The file contains many lines, all with the same basic pattern (see below), to execute all relevant files in sub-session.
This is the actual content of that file:
#!/bin/bash
~/${repo}/apt/security.sh
~/${repo}/apt/lemp.sh
~/${repo}/apt/certbot.sh
~/${repo}/apt/misc.sh
~/${repo}/third-party/pma.sh
~/${repo}/third-party/wp-cli.sh
~/${repo}/conf/nginx.sh
~/${repo}/conf/php.sh
~/${repo}/conf/crontab.sh
~/${repo}/local/tdm.sh
One can notice the repetitive ~/${repo}/
pattern.
It isn't a big problem, but it would still be good to reduce these redundant characters as this file should become larger.
What is the most minimal way possible to achieve a DRY (Don’t Repeat Yourself version of that code?
One single long line isn't something I personally would want to use, in this case.
Edit: By principle, there aren't and there shouldn't be any other files in the listed directories, besides the files listed in localize.sh
.
Also, it might be that the name localize.sh
, as well as calling the action of the file localization
is a bad approach; Please criticize me if you think it's bad, in a side note.
Best Answer
It’s not clear whether you’re running all the scripts in the relevant directories, but if you are, you might find
run-parts
useful:Note that this will run the scripts in alphanumeric order inside each directory, so if order is significant inside a subdirectory you’ll need to rename them (or number them). You can see what
run-parts
will do ahead of time by running it with the--test
option: