First Implementation of cd-mount.sh
This commit is contained in:
parent
478566553d
commit
3ee30d837f
1 changed files with 82 additions and 3 deletions
85
cd-mount.sh
Normal file → Executable file
85
cd-mount.sh
Normal file → Executable file
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
#=======================================================================
|
||||
# FILE: cd-mount.sh
|
||||
# USAGE: cd-mount.sh [<Verzeichnis, welches bearbeitet werden sollte>]
|
||||
# USAGE: cd-mount.sh [<Imageverzeichnis>]
|
||||
# DESCRIPTION:
|
||||
# OPTIONS:
|
||||
# REQUIREMENTS:
|
||||
|
@ -10,10 +10,89 @@
|
|||
# AUTHOR: Jonas Tobias Hopusch <hopusch.jonastobias@fh-swf.de>
|
||||
# COMPANY: Fachhochschule Südwestfalen
|
||||
# VERSION: 1
|
||||
# CREATED: 2023-01-08T14:10Z
|
||||
# CREATED: 2023-01-10T16:32Z
|
||||
# REVISION:
|
||||
#=======================================================================
|
||||
#-----------------------------------------------------------------------
|
||||
# Abort script if undefined veriables are referenced
|
||||
# and properly handle pipe failure
|
||||
#-----------------------------------------------------------------------
|
||||
set -uo pipefail
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
# COMMENT
|
||||
# Script configuration
|
||||
#-----------------------------------------------------------------------
|
||||
imageFileEnding='.image'
|
||||
defaultTargetDir="$HOME/CDs/"
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
# Check if fuseiso and fusermount are present
|
||||
#-----------------------------------------------------------------------
|
||||
if ! which 'fuseiso' >/dev/null 2>&1; then
|
||||
echo 'fuseiso is not installed'
|
||||
exit 1
|
||||
elif ! which 'fusermount' >/dev/null 2>&1; then
|
||||
echo 'fusermount is not installed'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
# Determine target directory
|
||||
#-----------------------------------------------------------------------
|
||||
if [ -v 1 ]; then
|
||||
targetDir="$1"
|
||||
else
|
||||
targetDir="$defaultTargetDir"
|
||||
fi
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
# Search for matching images
|
||||
#-----------------------------------------------------------------------
|
||||
|
||||
#shellcheck disable=SC2006
|
||||
readarray -t imageFiles \
|
||||
<<<"`find "$targetDir" -type f -iname "*$imageFileEnding"`" \
|
||||
>/dev/null 2>&1
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
# Per Image, do the following
|
||||
#-----------------------------------------------------------------------
|
||||
amountOfImages="${#imageFiles[@]}"
|
||||
successfullyMountedImages=0
|
||||
for image in "${imageFiles[@]}"; do
|
||||
# Count how many there are in total
|
||||
#((amountOfImages++))
|
||||
|
||||
# Validate that it can be accessed
|
||||
if ! [ -f "$image" ]; then
|
||||
echo "$image is not a regular file. Skipping."
|
||||
continue
|
||||
elif ! [ -r "$image" ]; then
|
||||
echo "$image is not readable. Skipping."
|
||||
continue
|
||||
fi
|
||||
|
||||
# Determine the location the image should be mounted to and try to ensure
|
||||
# it exists
|
||||
mountDirName="${image:0:((${#image} - ${#imageFileEnding}))}"
|
||||
if ! mkdir -p "$mountDirName"; then
|
||||
echo "Could not create mount destination directory $mountDirName. Skipping!"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Check if there's already an image mounted at the destination
|
||||
#shellcheck disable=SC2006
|
||||
fullPathMountDir="`cd "$mountDirName" && pwd`"
|
||||
if grep "^fuseiso $fullPathMountDir" /proc/mounts >/dev/null 2>&1; then
|
||||
echo "Something is already mounted to $mountDirName. Skipping"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Attempt to mount the image to the destination directory
|
||||
if fuseiso "$image" "$mountDirName"; then
|
||||
echo "$image has been mounted to $mountDirName"
|
||||
((successfullyMountedImages++))
|
||||
fi
|
||||
done
|
||||
|
||||
echo "$successfullyMountedImages/$amountOfImages images have been mounted successfully"
|
||||
|
|
Loading…
Reference in a new issue