Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
The following answer was given by SE user Edward. The original source can be found here. The other answer gave some really good advice; this is intended as a complementary answer with still more...
#1: Initial revision
_The following answer was given by SE user [Edward](https://codereview.stackexchange.com/users/39848). The original source can be found [here](https://codereview.stackexchange.com/a/210317/98306)._
<hr />
The other answer gave some really good advice; this is intended as a complementary answer with still more things to think about.
## Put default arguments at the top of the script
If someone wanted to change the default arguments, they'd have to hunt through the code to find them. I typically prefer to put them at the top of the script and then only overwrite them if command line arguments are passed. For example:
#!/bin/bash
# default arguments
TARGET=./target
JSON=steps.json
# Command line args are both optional: TARGET JSON
if [[ -z "$1" ]] ; then
TARGET="$1"
fi
if [[ -z "$2" ]] ; then
JSON="$2"
fi
## Use `install` to copy files
DOS archives may or may not have proper permissions bits set and may need to have a complex path created before copying the file. We can manage all of this easily with `install` which is also a basic part of every Linux installation:
echo "installing $src on $disk to $dst"
install -p --mode=664 -D "$TMPDIR"/$src -t "$TARGET"/$dst/
With the `-p` argument we preserve the original timestamp. The mode argument explictly sets the `mode` for each file (you could, of course change this to something else if you cared to). The combination of `-D` and `-t` tells install to create the destination directory if it doesn't already exist.
## Do more with `jq`
Since you're already requiring a dependency on `jq`, it makes sense to use its capabilities more thoroughly. As you know, it has the ability to apply one or more filters sequentially to the result of the previous step. We can use this to great advantage and only call `jq` once like this:
# use jq to create disk, src, dst triplets to feed to inst
jq -r -c '.[] | {disk, file: .files[]} | {disk, src: .file.src, dst: .file.dest} | [.disk,.src,.dst] |@sh ' "$JSON" | while read line
do inst ${line}
done
As you can see from the comment, this extracts `(disk, src, dst)` triplets.
## Create a function to do the work
Given the above advice, what we need is the `inst` routine to actually do the work. Here's one way to write that:
# working variables
TMPDIR=
LASTDISK=
# given disk, src, dst triplet
# mount the disk in a temporary dir
# (if not already mounted)
# and install from src to dst
# src may contain wildcards
function inst () {
disk=$(eval echo $1)
src=$(eval echo $2)
dst=$(eval echo $3)
if [[ "$disk" != "$LASTDISK" ]] ; then
cleanup
TMPDIR="$(mktemp -d)"
echo "mounting $disk on $TMPDIR"
if sudo mount -r "$disk" "$TMPDIR" ; then
LASTDISK="$disk"
else
echo "Failed to mount $disk"
sudo rmdir "$TMPDIR"
fi
fi
echo "installing $src on $disk to $dst"
install -p --mode=664 -D "$TMPDIR"/$src -t "$TARGET"/$dst/
}
Notice that I've used a number of `bash`-isms here that make this non-portable, but since you've explicitly called out `bash`, I'm assuming this is OK. I've also chosen to use `sudo mount` and `sudo umount` instead of `udisksctl`. Either could work, of course; it's a matter of preference as to which is used. On one hand, `mount` is always available but on the other, it requires `sudo` privileges. Most of this will be self-explanatory, except for cleanup which is described in the next suggestion.
## Use a cleanup function
It's annoying when a script fails for some reason and then leaves temporary files or other junk lying around as a result. One technique that's handy for this is to use `bash`'s `TRAP` feature.
# un mount and remove bind dir TMPDIR if
# TMPDIR is not empty
function cleanup {
if [[ ! -z "$TMPDIR" ]] ; then
sudo umount "$TMPDIR"
sudo rm -rf "$TMPDIR"
fi
}
# rest of script ...
trap cleanup EXIT
This tells `bash` that no matter how we get to the exit (either normally or via some fatal error) it needs to invoke the specified function, which I typically name `cleanup` for obvious reasons.
