[EdLUG] Bash Scripts

Keith Wyse kwyse3436 at gmail.com
Mon Jan 3 13:33:38 UTC 2022


Hi all;
I hope your New Year is going well so far.
Recently I have been doing some Bash scripting on my Ubuntu 21.04 desktop,
and found I was repeating similar commands in different scripts.
Though I have been using Bash for quite a while, I'm no expert and am still
learning all the time.

I found the 'source' command which brings in another file as if it were
part of the script, and have been using that in a similar way to a library
in a programming language.
I have built a few of these templates as I call them, and then also
combined templates into profiles; a script that only needs a log file can
source profile.logfile, a different script which needs to check sudo
permissions sources profile.all. Each of these pull in just enough for the
actions to be done.
Also at the top of each template/profile I have added a commented out
section which shows the normal use, so that this can easily be copied to a
script being written.

Then I realised that I have some scripts which call other scripts, and
pulling in templates/profiles in the called script may change set
variables. To handle that I added an 'if' condition to test if the variable
is already set. And at the end I have added another 'if' to test if the
script is the top level, only then is the variable unset in the tidy-up.

Further I have added functions which test if the set variable is correct
and I try to have a function 'template.valid' in each template which makes
all necessary checks on the variable.
Yes this might seem to be unnecessarily pedantic, but it is a learning
exercise as well as functional.

Linked is a small file template.cmndname which simply sets a variable
according to what script is being executed. In essence it's only a single
line, but the template goes further & checks it is valid. This template is
normally set in each script whether or not it is called from another
script, as I use this in messages.

Let me know if the link works okay.
Interested in people's comments & suggestions.

Cheers
Keith

https://drive.google.com/file/d/1H6jc5lYwLxmRaDU7FGNHm9dDJXpYS-78/view?usp=sharing
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.edlug.org.uk/pipermail/edlug/attachments/20220103/e95d5f33/attachment.htm>


More information about the EdLUG mailing list